栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从Python脚本将数据插入MySQL表

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从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()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/660093.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号