MySQLdb Python execute not returning选项卡

2024-06-16 10:48:05 发布

您现在位置: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端输出:

()

请告诉我问题是什么,怎么解决。谢谢您!你知道吗

-希杰


Tags: iddbdatemytablelinknotsummary
1条回答
网友
1楼 · 发布于 2024-06-16 10:48:05

请将每个SQL命令分隔为单独的execute()调用。你知道吗

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

相关问题 更多 >