MySQLdb Python执行不返回Table

时间:2014-08-12 02:03:02

标签: python mysql mysql-python

我一直在尝试使用python的MySQLdb在我的webhost上通过SSH在MySQL数据库上执行SQL。我写的这个程序(在Mac上)应该打印一张表,但它没有。

这是我的代码:

import MySQLdb

db = MySQLdb.connect("my host","my username","my password","my database")

cursor = db.cursor()


cursor.execute('''
    DROP TABLE IF EXISTS news;
    CREATE TABLE news
    (
        id int unsigned NOT NULL auto_increment,
        headline varchar(250) NOT NULL,
        summary varchar(5000) NOT NULL,
        date varchar(50) NOT NULL,
        link varchar(2500) NOT NULL,
        imagelink varchar(2500) NOT NULL,
        category varchar(50) NOT NULL,

        PRIMARY KEY (id)
    );

    insert into news (headline, summary, date, link, imagelink, category)
    values ("The worlds awesomest news source.", "Released by So-and-so , this will be the greatest thing ever.", "Aug. 11", "http://www.google.com/", "http://www.example.com/", "World");

    SELECT summary FROM news WHERE id = 1;
    ''')


results = cursor.fetchall()
print results

... Mac终端的输出是:

()

请告诉我问题是什么,以及如何解决。谢谢!

-CJ

1 个答案:

答案 0 :(得分:0)

请将每个SQL命令分成对execute()的单独调用。

以这种方式组合多个SQL命令可能不起作用,并且分离成多个execute()命令使调试更容易。

相关问题