使用sqlite3和python连接数据库时出错

时间:2018-09-26 16:49:04

标签: sqlite python-3.5

在运行以下代码时,出现错误,我将其发布在帖子底部。我遵循了有关从here创建数据库的教程。 这些功能在创建以前的数据库时有效。

我正在使用jupyter notebook v 3.5。

def create_connection(db_file):

  try:
    conn = sqlite3.connect(db_file)
    return conn
  except sqlite3.Error as e:
    print("Connection error: [%s]" % e)

  return None

def create_table(conn, create_table_sql ):

  try:
    c = conn.cursor()
    c.execute(create_table_sql)
  except sqlite3.Error as e:
    print("Connection error while creating table: [%s]" % e)

def sqlTables(db_file):

  sql_create_synset_table = ''' CREATE TABLE IF NOT EXISTS table_data (

                                    id TEXT NOT NULL,
                                    status TEXT NOT NULL,
                                    confidence_score INT NOT NULL,
                                                ); '''
  conn = create_connection(db_file)
  if conn is not None:
    create_table(conn,sql_create_synset_table)
  else:
    print("Error! cannot create db conn.")

def upload_data(db_file):
 sqlTables(db_file)
 conn = create_connection(db_file)
 cursor = conn.cursor()

 with conn:
    for i in range(len(id_list)):
        s_id = id_list[i]
        status = status_list[i]
        conf = conf_list[i]  

        cursor.execute("INSERT INTO table_data(id, status, confidence_score) VALUES(?,?,?)"\
                                                    ,(s_id, status, conf))
        conn.commit()

upload_data("path/to/db/table.db")
  

创建表时出现连接错误:[附近“)”:语法错误]

     

---> 12 cursor.execute(“ INSERT INTO table_data(id,status,confidence_score)VALUES(?,?,?)”,(sset_id,stus,conf))

     

OperationalError:没有这样的表:table_data

0 个答案:

没有答案