无法使用 python 的 pymysql 将 csv 文件导入 sql 服务器
not able to import the csv file into sql server using python's pymysql
我有一个非常简单的 csv 文件,有 3 列,名称为 'a'、'b'、'c',整数类型有 5 列。我想使用 pymysql 将此数据导入数据库中的 SQL 服务器。有人可以为我提供代码吗?还需要 PHPMyAdmin 才能执行此操作吗?
这是我的实际代码:
import pymysql
f = open(r"a.csv", "r")
fString = f.read()
print(fString)
fList = []
for line in fString.split('\n'):
fList.append(line.split(','))
del(fList[0])
conn = pymysql.connect(host='localhost', user='root',
password='ajit2910@', database='mydatabase')
cur = conn.cursor()
cur.execute('CREATE TABLE jogi4(a INT,b INT,c INT)')
for i in range(len(fList)-1):
sqlquery = "INSERT INTO jogi3(a,b,c) VALUES(%s,%s,%s)"
cur.execute(sqlquery, (fList[i][0], fList[i][1], fList[i][2])) conn.close()
基本上,您的问题是您正在创建 table jogi4
并插入 jogi3
。不过我正在写一个更详细的答案。
这是带有注释的更正代码。
基本上:
- 改进:使用 CSV 模块避免解析
- 修复:到处使用
jogi4
table
- 修复:COMMIT SQL 事务。
import pymysql
import csv
# create the connection BEFORE to avoid recreating it at each loop
conn = pymysql.connect(host='localhost', user='root', database='Whosebug')
cur = conn.cursor()
# Adds an 'IF NOT EXISTS' clause to avoid an Exception if the table already exists
cur.execute('CREATE TABLE IF NOT EXISTS jogi4(a INT,b INT,c INT)')
csvfile = open('a.csv')
# Use a CSV reader to avoid having to parse the CSV file myself
reader = csv.reader(csvfile)
# Skip the header
next(reader, None)
for row in reader:
sqlquery = "INSERT INTO jogi4(a,b,c) VALUES(%s,%s,%s)"
s = cur.execute(sqlquery, (row[0], row[1], row[2]))
cur.close()
# You forgot to commit the inserts, see "Transactions" in SQL
conn.commit()
conn.close()
我有一个非常简单的 csv 文件,有 3 列,名称为 'a'、'b'、'c',整数类型有 5 列。我想使用 pymysql 将此数据导入数据库中的 SQL 服务器。有人可以为我提供代码吗?还需要 PHPMyAdmin 才能执行此操作吗?
这是我的实际代码:
import pymysql
f = open(r"a.csv", "r")
fString = f.read()
print(fString)
fList = []
for line in fString.split('\n'):
fList.append(line.split(','))
del(fList[0])
conn = pymysql.connect(host='localhost', user='root',
password='ajit2910@', database='mydatabase')
cur = conn.cursor()
cur.execute('CREATE TABLE jogi4(a INT,b INT,c INT)')
for i in range(len(fList)-1):
sqlquery = "INSERT INTO jogi3(a,b,c) VALUES(%s,%s,%s)"
cur.execute(sqlquery, (fList[i][0], fList[i][1], fList[i][2])) conn.close()
基本上,您的问题是您正在创建 table jogi4
并插入 jogi3
。不过我正在写一个更详细的答案。
这是带有注释的更正代码。
基本上:
- 改进:使用 CSV 模块避免解析
- 修复:到处使用
jogi4
table - 修复:COMMIT SQL 事务。
import pymysql
import csv
# create the connection BEFORE to avoid recreating it at each loop
conn = pymysql.connect(host='localhost', user='root', database='Whosebug')
cur = conn.cursor()
# Adds an 'IF NOT EXISTS' clause to avoid an Exception if the table already exists
cur.execute('CREATE TABLE IF NOT EXISTS jogi4(a INT,b INT,c INT)')
csvfile = open('a.csv')
# Use a CSV reader to avoid having to parse the CSV file myself
reader = csv.reader(csvfile)
# Skip the header
next(reader, None)
for row in reader:
sqlquery = "INSERT INTO jogi4(a,b,c) VALUES(%s,%s,%s)"
s = cur.execute(sqlquery, (row[0], row[1], row[2]))
cur.close()
# You forgot to commit the inserts, see "Transactions" in SQL
conn.commit()
conn.close()