Python不插入数据';s进入mysql
我正在测试Python和Mysql,因为我可以创建和删除表,但无法在其中插入数据Python不插入数据';s进入mysql,python,mysql,sql,Python,Mysql,Sql,我正在测试Python和Mysql,因为我可以创建和删除表,但无法在其中插入数据 commit() 所以我使用了它,即使在我使用之后数据也没有插入到数据库中。请帮助我。 这是我使用的代码,它创建表,但不插入数据 import MySQLdb db = MySQLdb.connect("localhost","user","password") cxn = MySQLdb.connect(db='test') cursor = cxn.cursor() cursor.execute("CREAT
commit()
所以我使用了它,即使在我使用之后数据也没有插入到数据库中。请帮助我。
这是我使用的代码,它创建表,但不插入数据
import MySQLdb
db = MySQLdb.connect("localhost","user","password")
cxn = MySQLdb.connect(db='test')
cursor = cxn.cursor()
cursor.execute("CREATE TABLE users(name VARCHAR(40),id VARCHAR(40))")
cursor.execute("INSERT INTO users(name,id) VALUES('John','1')")
db.commit()
print "Opertion completed successfully"
db
和cxn
是否连接到同一数据库
您应该使用以下方法建立连接:
db = MySQLdb.connect(host="localhost",
db="test",
user="user",
passwd="password")
然后,应通过以下方式从该连接导出光标:
cursor = db.cursor()
我敢说,您的问题来自db
和cxn
之间的歧义