0

这是我的代码,我在 ipython 笔记本内的 Windows XP 上运行它。

import os
os.getcwd() # This is current working directory 

db_path = '..\\06_django\\hzmo_web\\hzmo_web\\hzmo_backup_05_2013.db'


import sqlite3
from pandas.io import sql
# Create your connection.
cnx = sqlite3.connect('db_path')

cnx

cur = cnx.cursor()    
cur.execute('SELECT SQLITE_VERSION()')
data = cur.fetchone()
print "SQLite version: %s" % data 

import contextlib
with contextlib.closing(sqlite3.connect(db_path)) as conn:
    conn.row_factory = sqlite3.Row
    cursor = conn.cursor()    
    cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
    for tablerow in cursor.fetchall():
        print tablerow[0]
        #table = tablerow[0]
        #cursor.execute("SELECT * FROM {t}".format(t = table))
        #for row in cursor:
        #    for field in row.keys():
        #        print(table, field, row[field])

sql.read_frame('SELECT * FROM hzmo_report;', cnx)

我只想从 pandas DataFrame 中的 hzmo_report 表中选择所有数据,正如我在http://pandas.pydata.org/pandas-docs/stable/io.html#sql-queries的文档中看到的那样

我得到的错误是: OperationalError: no such table: hzmo_report 但该表确实存在。

Here is output of my code:
---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
<ipython-input-8-916237d66737> in <module>()
     29         #        print(table, field, row[field])
     30 
---> 31 sql.read_frame('SELECT * FROM hzmo_report;', cnx)
     32 

C:\Documents and Settings\hr1ub098\Application Data\Python\Python27\site-packages\pandas\io\sql.pyc in read_frame(sql, con, index_col, coerce_float)
    141         column name to use for the returned DataFrame object.
    142     """
--> 143     cur = execute(sql, con)
    144     rows = _safe_fetch(cur)
    145     columns = [col_desc[0] for col_desc in cur.description]

C:\Documents and Settings\hr1ub098\Application Data\Python\Python27\site-packages\pandas\io\sql.pyc in execute(sql, con, retry, cur, params)
     33 
     34         if params is None:
---> 35             cur.execute(sql)
     36         else:
     37             cur.execute(sql, params)

OperationalError: no such table: hzmo_report

SQLite version: 3.6.21
auth_permission
auth_group_permissions
auth_group
auth_user_user_permissions
auth_user_groups
auth_user
django_content_type
django_session
django_site
django_admin_log
hzmo_report
Error on sql SELECT * FROM hzmo_report;

有谁知道可能是什么问题?

4

1 回答 1

2
cnx = sqlite3.connect('db_path')

该参数不应该是您创建的变量,而不是字符串吗?在上述情况下,您可能正在连接到一个名为“db_path”的新数据库。

cnx = sqlite3.connect(db_path)
于 2013-05-03T13:14:05.817 回答