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

2021-11-20 00:00:00 python mysql

我想在我的 MySQL 数据库中插入整数 188 和 90,但以下代码不起作用:

导入 MySQLdbconn = MySQLdb.connect(host="localhost",用户 =root",passwd="新密码",db="engy1")x = conn.cursor()x.execute("SELECT * FROM anooog1")x.execute (" INSERT INTO anooog1 VALUES ('%s','%s') ", (188,90))行 = x.fetchall()

为什么它不起作用?

解决方案

#Server Connection to MySQL:导入 MySQL 数据库conn = MySQLdb.connect(host="localhost",用户 =root",passwd="新密码",db="engy1")x = conn.cursor()尝试:x.execute("""插入 anooog1 值 (%s,%s)""",(188,90))conn.commit()除了:conn.rollback()conn.close()

编辑为我工作:

<预><代码>>>>导入 MySQL 数据库>>>#连接数据库... db = MySQLdb.connect("localhost","root","password","testdb")>>>>>>#设置光标... 游标 = db.cursor()>>>>>>#创建anooog1表... cursor.execute("DROP TABLE IF EXISTS anooog1")__main__:2: 警告:未知表 'anooog1'0升>>>>>>sql = """创建表 anooog1 (... COL1 INT,... COL2 INT )""">>>游标.执行(sql)0升>>>>>>#插入表格... 尝试:... cursor.execute("""INSERT INTO anooog1 VALUES (%s,%s)""",(188,90))... db.commit()... 除了:... db.rollback()...1L>>>#显示表... cursor.execute("""SELECT * FROM anooog1;""")1L>>>打印 cursor.fetchall()((188L, 90L),)>>>>>>db.close()

mysql中的表;

mysql>使用测试数据库;读取表信息以补全表名和列名您可以关闭此功能以使用 -A 更快地启动数据库已更改mysql>SELECT * FROM anooog1;+------+------+|COL1 |COL2 |+------+------+|188 |90 |+------+------+1 行(0.00 秒)mysql>

I want to insert the integers 188 and 90 in my MySQL database, but the following code doesn't work:

import MySQLdb
conn = MySQLdb.connect(host= "localhost",
                  user="root",
                  passwd="newpassword",
                  db="engy1")
x = conn.cursor()
x.execute("SELECT *  FROM anooog1")
x.execute (" INSERT INTO anooog1 VALUES ('%s','%s') ", (188,90))
row = x.fetchall()

Why doesn't it work?

解决方案

#Server Connection to MySQL:

import MySQLdb
conn = 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()

edit working for me:

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

table in mysql;

mysql> use testdb;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> SELECT * FROM anooog1;
+------+------+
| COL1 | COL2 |
+------+------+
|  188 |   90 |
+------+------+
1 row in set (0.00 sec)

mysql> 

相关文章