从Python脚本将数据插入MySQL表

从Python脚本将数据插入MySQL表,第1张

从Python脚本将数据插入MySQL表

您可以将MySQLdb用于Python。

示例代码(您需要对其进行调试,因为我无法在此处运行它):

#!/usr/bin/pythonimport MySQLdb# Open database connectiondb = MySQLdb.connect("localhost","testuser","test123","TESTDB" )# prepare a cursor object using cursor() methodcursor = db.cursor()# Select qSQL with id=4.cursor.execute("SELECT qSQL FROM TBLTEST WHERe id = 4")# Fetch a single row using fetchone() method.results = cursor.fetchone()qSQL = results[0]cursor.execute(qSQL)# Fetch all the rows in a list of lists.qSQLresults = cursor.fetchall()for row in qSQLresults:    id = row[0]    city = row[1]    #SQL query to INSERT a record into the table FACTRESTTBL.    cursor.execute('''INSERT into FACTRESTTBL (id, city)       values (%s, %s)''',       (id, city))    # Commit your changes in the database    db.commit()# disconnect from serverdb.close()


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5662160.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存