python-2.7 - Python MySQLdb不插入数据

标签 python-2.7 mysql-python ubuntu-12.10

ubuntu版本:12.10
mysql服务器版本:5.5.29-0
python版本:2.7

我正在尝试使用 MySQLdb 将数据插入我的本地主机 mysql 服务器。运行脚本时我没有收到任何错误,但数据没有输入到我的表中。我使用 phpmyadmin 查看表格。

我尝试回到基础知识并遵循教程,但结果相同。奇怪的是我可以创建和删除表,但不能输入数据。

该代码来自教程,甚至报告插入了 4 行。当脚本报告一切正常时,是什么阻止数据输入表中?

cursor = conn.cursor () 
cursor.execute ("DROP TABLE IF EXISTS animal") 
cursor.execute (""" 
        CREATE TABLE animal 
        ( 
            name CHAR(40), 
            category CHAR(40) 
        ) 
    """) 
cursor.execute (""" 
        INSERT INTO animal (name, category) 
        VALUES 
            ('snake', 'reptile'), 
            ('frog', 'amphibian'), 
            ('tuna', 'fish'), 
            ('racoon', 'mammal') 
    """) 
print "%d rows were inserted" % cursor.rowcount 

最佳答案

添加:

conn.commit()

位于脚本底部。

附带说明一下,请查看以下内容:http://mysql-python.sourceforge.net/MySQLdb.html

关于python-2.7 - Python MySQLdb不插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14929318/

相关文章:

python - Flask 应用程序中的 Zerorpc 在 apache 服务器中启动时无法工作,但在由 pycharm 启动时工作正常

python - mysql 和 Python : does not allow two timestamp columns

docker - 使用 docker-compose 从 Flask 应用程序连接到 MySQL

mysql - 使用 solr 和 tomcat6 的数据导入问题(HTTP 状态 404)

php - Wordpress 永久链接永远无法在本地主机 Ubuntu 12.10 上运行

python - 如何从 StringIO 对象中删除第一个字符?

python-2.7 - 在 ZPublisher.HTTPRequest.FileUpload 上使用 is_zipfile

Python 单词计数器

mysql - apache airflow initdb 在 mysql 的 kubernetes_resource_checkingpoint 失败

memory - 限制 64 位 Ubuntu 12.10 上的可用内存