我如何用Python插入许多MYSQL列?

时间:2017-06-21 08:52:08

标签: python mysql

我有一个包含数据的文件:

param1 = value1
param2 = value2
....
param100 = value100

另外,我有一个MYSQL表看起来像这样:

      Param1 Param2 ... Param100
Row0  Value1 Value2 ... Value3

我必须解析我的数据文件并插入一个由许多列组成的行。 我成功解析了一个文件并创建了一个列表。 任何建议如何插入? 目前我有:

db = MySQLdb.connect(host="**",port=**,user="**",passwd="***",db="**") 
# prepare a cursor object using cursor() method
cursor = db.cursor()
params = ""
values = ""
with open(filename) as inf:
    for line in inf:
        words = line.split(' ')
        if len(words) == 3:
      params+=words[0]
      params+=","
      values+=words[2].rstrip()
      values+=","
    # Prepare SQL query to INSERT a record into the database.

params = params[:-1]
values = values[:-1]

sql = """INSERT INTO BE_DB_status("""+params+""") VALUES ("""+values+""")s)"""
print sql
cursor.execute(sql)
db.commit()
enter code here

0 个答案:

没有答案