Python 和 MySQL 的问题

发布于 2024-11-09 14:10:38 字数 1381 浏览 3 评论 0原文

我正在尝试将一个大文本文件导入 MySQL 数据库。 SQL 语句如下:

LOAD DATA INFILE '/tmp/epf/full/album_popularity_per_genre' 
INTO TABLE album_popularity_per_genre 
CHARACTER SET UTF8 FIELDS TERMINATED BY X'01' LINES TERMINATED BY '\n' 
IGNORE 45 LINES (export_date, storefront_id, genre_id, album_id, album_rank)

当我在 phpMyAdmin 中运行上面的代码时,上面的代码有效,但是当我在 Python 中编写一个使用上述 SQL 语句的简单函数时,我收到错误。

这是Python代码,

def test():
    dbConnection = MySQLdb.connect(
    charset='utf8', 
    host='localhost', 
    user='root', 
    passwd='root', 
    db='epf')

    cursor = dbConnection.cursor()

    exStr = """LOAD DATA INFILE '/tmp/epf/full/album_popularity_per_genre' 
               INTO TABLE album_popularity_per_genre CHARACTER SET UTF8 
               FIELDS TERMINATED BY X'01' LINES TERMINATED BY '\n' 
               IGNORE 45 LINES 
               (export_date, storefront_id, genre_id, album_id, album_rank)"""

    try:
        cursor.execute(exStr)
    except MySQLdb.Warning, e:
        print "Warning %s" % (str(e))
    except MySQLdb.IntegrityError, e:     
        print "Error %d: %s" % (e.args[0], e.args[1])

    #Clean up
    cursor.close()
    dbConnection.close()

我得到的错误如下,

Warning Data truncated for column 'album_rank' at row 1

我现在的问题是,为什么原始SQL语句可以工作,但当我尝试运行Python代码时,没有数据导入数据库?

I am trying to import a large text file into a MySQL database. The SQL statement is as follows:

LOAD DATA INFILE '/tmp/epf/full/album_popularity_per_genre' 
INTO TABLE album_popularity_per_genre 
CHARACTER SET UTF8 FIELDS TERMINATED BY X'01' LINES TERMINATED BY '\n' 
IGNORE 45 LINES (export_date, storefront_id, genre_id, album_id, album_rank)

The above works when I run it in phpMyAdmin, however when I write a simple function in Python that uses the above SQL statement I get an error.

Here is the Python code,

def test():
    dbConnection = MySQLdb.connect(
    charset='utf8', 
    host='localhost', 
    user='root', 
    passwd='root', 
    db='epf')

    cursor = dbConnection.cursor()

    exStr = """LOAD DATA INFILE '/tmp/epf/full/album_popularity_per_genre' 
               INTO TABLE album_popularity_per_genre CHARACTER SET UTF8 
               FIELDS TERMINATED BY X'01' LINES TERMINATED BY '\n' 
               IGNORE 45 LINES 
               (export_date, storefront_id, genre_id, album_id, album_rank)"""

    try:
        cursor.execute(exStr)
    except MySQLdb.Warning, e:
        print "Warning %s" % (str(e))
    except MySQLdb.IntegrityError, e:     
        print "Error %d: %s" % (e.args[0], e.args[1])

    #Clean up
    cursor.close()
    dbConnection.close()

The error I get is as follows,

Warning Data truncated for column 'album_rank' at row 1

My question now is, why does the raw SQL statement work but when I try to run the Python code, no data is imported into the database?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情勿念 2024-11-16 14:10:38

Python DBAPI 是隐式事务性的。尝试在执行后添加 dbConnection.commit() 。

The Python DBAPI is implicitly transactional. Try adding dbConnection.commit() after the execute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文