Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python数据库插入MySQL_Python_Mysql_Mysql Python_Sql Insert - Fatal编程技术网

Python数据库插入MySQL

Python数据库插入MySQL,python,mysql,mysql-python,sql-insert,Python,Mysql,Mysql Python,Sql Insert,我正在尝试用Python和MySQL创建一个表,然后在表中插入新值 程序运行时没有错误消息,但表始终为空。以下是我的python脚本: cursor = conn.cursor() cursor.execute('DROP TABLE twitter') cursor.execute("CREATE TABLE twitter (id INT UNSIGNED NOT NULL AUTO_INCREMENT, user_id CHAR, state CHAR(2), city CHAR, twe

我正在尝试用Python和MySQL创建一个表,然后在表中插入新值

程序运行时没有错误消息,但表始终为空。以下是我的python脚本:

cursor = conn.cursor()
cursor.execute('DROP TABLE twitter')
cursor.execute("CREATE TABLE twitter (id INT UNSIGNED NOT NULL AUTO_INCREMENT, user_id CHAR, state CHAR(2), city CHAR, tweet CHAR, PRIMARY KEY (id))")

### after getting some data, then insert the data into the table: 
cursor.execute('INSERT INTO twitter VALUES ({0},{1},{2},{3})'.format(tw_userid, tw_state, tw_city, tw_text))
cursor.close()
conn.commit()
conn.close()
但是,每当我试图从这个表中选择数据时,都会得到一个空集

我也尝试不使用格式选项,但仍然得到一个空集:

cursor.execute('INSERT INTO twitter VALUES ({0},{1},{2},{3})', (tw_userid, tw_state, tw_city, tw_text))

知道为什么insert命令不起作用吗;在python中,您需要使用逗号来分隔参数

cursor.execute('INSERT INTO twitter VALUES ({0},{1},{2},{3})'.format(tw_userid, tw_state, tw_city, tw_text))
应该是:

data = (tw_userid, tw_state, tw_city, tw_text)
cursor.execute('INSERT INTO twitter VALUES ({0},{1},{2},{3})', data)

您可以在上看到python连接器的文档。

我做了更改,但仍然得到空表@瓦希德米尔:看看我最新的答案。您可以阅读更多关于
format()