将python文件写入联机SQL数据库

时间:2022-05-21 01:32:44

I'm trying to take my output from my python code and write it into a chart on an online SQL database. I attempted to use sqlite3 here:

我正在尝试从我的python代码中获取输出并将其写入在线SQL数据库的图表中。我试图在这里使用sqlite3:

def write_sql(index, data):


    conn = sqlite3.connect('tt.db')

    c = conn.cursor()
    #c.execute('''CREATE TABLE IPlist(IP text, Availibility real)''')

    for key in data:    
        print key
        c.execute("INSERT INTO IPlist VALUES (%s,%d)" , (key, data[key]))

    conn.commit()

However, I get an operational error and I'm also unsure of how to connect to the online database as opposed to just a local test document.

但是,我遇到了操作错误,我也不确定如何连接到在线数据库,而不仅仅是本地测试文档。

1 个解决方案

#1


Assuming that you're using the standard sqlite3 library, then you're specifying the parameters to the query incorrectly. From the documentation:

假设您正在使用标准的sqlite3库,那么您将错误地指定查询的参数。从文档:

The sqlite3 module supports two kinds of placeholders: question marks (qmark style) and named placeholders (named style).

sqlite3模块支持两种占位符:问号(qmark样式)和命名占位符(命名样式)。

You're attempting to use ANSI C printf format codes, which is not supported. Try either of the following:

您正在尝试使用不受支持的ANSI C printf格式代码。请尝试以下任一操作:

c.execute("INSERT INTO IPlist VALUES (?,?)" , (key, data[key]))

c.execute("INSERT INTO IPlist VALUES (:key,:data)" , {"key":key, "data":data[key]})

#1


Assuming that you're using the standard sqlite3 library, then you're specifying the parameters to the query incorrectly. From the documentation:

假设您正在使用标准的sqlite3库,那么您将错误地指定查询的参数。从文档:

The sqlite3 module supports two kinds of placeholders: question marks (qmark style) and named placeholders (named style).

sqlite3模块支持两种占位符:问号(qmark样式)和命名占位符(命名样式)。

You're attempting to use ANSI C printf format codes, which is not supported. Try either of the following:

您正在尝试使用不受支持的ANSI C printf格式代码。请尝试以下任一操作:

c.execute("INSERT INTO IPlist VALUES (?,?)" , (key, data[key]))

c.execute("INSERT INTO IPlist VALUES (:key,:data)" , {"key":key, "data":data[key]})