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

如何将数据插入MySQL数据库?

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

如何将数据插入MySQL数据库?

#Server Connection to MySQL:import MySQLdbconn = MySQLdb.connect(host= "localhost",       user="root",       passwd="newpassword",       db="engy1")x = conn.cursor()try:   x.execute("""INSERT INTO anooog1 VALUES (%s,%s)""",(188,90))   conn.commit()except:   conn.rollback()conn.close()

编辑 为我工作:

>>> import MySQLdb>>> #connect to db... db = MySQLdb.connect("localhost","root","password","testdb" )>>> >>> #setup cursor... cursor = db.cursor()>>> >>> #create anooog1 table... cursor.execute("DROp TABLE IF EXISTS anooog1")__main__:2: Warning: Unknown table 'anooog1'0L>>> >>> sql = """CREATE TABLE anooog1 (...          COL1 INT,  ...          COL2 INT )""">>> cursor.execute(sql)0L>>> >>> #insert to table... try:...     cursor.execute("""INSERT INTO anooog1 VALUES (%s,%s)""",(188,90))...     db.commit()... except:     ...     db.rollback()... 1L>>> #show table... cursor.execute("""SELECT * FROM anooog1;""")1L>>> print cursor.fetchall()((188L, 90L),)>>> >>> db.close()

在MySQL表;

mysql> use testdb;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> SELECt * FROM anooog1;+------+------+| COL1 | COL2 |+------+------+|  188 |   90 |+------+------+1 row in set (0.00 sec)mysql>


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

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

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