|||
python利用pymysql load file需要进行显示提交,数据库才有变化。
import os import pymysql import pymysql.cursors db = pymysql.connect( host='YOURHOST', port=YOURPORT, user='USER', passwd='YOUR_PASSWORD', local_infile=True ) cur = db.cursor() #cur.execute('CREATE DATABASE IF NOT EXISTS dbtest;') cur.execute('USE dbtest;') cur.execute('SHOW DATABASES;') result = cur.fetchall() print(result) sql = """CREATE TABLE IF NOT EXISTS EMPLOYEE ( FIRST_NAME CHAR(40) NOT NULL, LAST_NAME CHAR(40), AGE INT, SEX CHAR(10), INCOME FLOAT)""" cur.execute(sql) # Note: Windows下换行符为‘\r\n’,Linux系统下换行符为‘\n’ # This script is wrote for Windows. sql= '''load data local infile 'YOUR_PATH/2.txt' ignore into table employee fields terminated by ',' lines terminated by '\r\n' ; ''' cur.execute(sql) # 需显示提交 db.commit() db.close()
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-11-18 13:26
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社