使用 python 在 sql 服务器中插入数据
Insert data in sql server with python
我想从 raspberry pi 和 python 的 sql 服务器中插入数据。
我尝试使用 pypyodbc,但它无法正常工作。
你能指导我使用 模块吗?
import pyodbc
conn = pyodbc.connect(
'DRIVER={SQL Server Native Client 11.0};'
'SERVER=server;'
'Integrated_Security=false;'
'Trusted_Connection=no;'
'UID=pi;'
'PWD=pi;'
'DATABASE= database'
)
cursor = conn.cursor()
cursor.execute('SELECT * FROM database.table')
for row in cursor:
print(row)
yodbc.InterfaceError: ('28000', '[28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user \'pi\'. (18456) (SQLDriverConnect); [28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database " database" requested by the login. The login failed. (4060); [28000] [Microsoft][SQL Server Native Client 11.0]Invalid connection string attribute (0); [28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user \'pi\'. (18456); [28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database " database" requested by the login. The login failed. (4060); [28000] [Microsoft][SQL Server Native Client 11.0]Invalid connection string attribute (0)')
我强烈推荐使用 sqlite3
import sqlite3
conn = sqlite3.connect(
'DRIVER={SQL Server Native Client 11.0};'
'SERVER=server;'
'Integrated_Security=false;'
'Trusted_Connection=no;'
'UID=pi;'
'PWD=pi;'
'DATABASE= database'
)
cursor = conn.cursor()
cursor.execute('SELECT * FROM database.table')
for row in cursor:
print(row)
所以一位同事刚从我身边经过,告诉我将所有数据放在 '' 之间,仅在 1 行中
我还删除了'Integrated_Security=false;'Trusted_Connection=no;有人告诉我
工作代码
something='something'
import pyodbc
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=srv;DATABASE=datavase;UID=pi;PWD=pass')
cursor = conn.cursor()
cursor.execute("insert into test values (?, 'awesome library ')",var)
cursor.execute('SELECT * FROM test')
for row in cursor:
print(row)
我想从 raspberry pi 和 python 的 sql 服务器中插入数据。 我尝试使用 pypyodbc,但它无法正常工作。 你能指导我使用 模块吗?
import pyodbc
conn = pyodbc.connect(
'DRIVER={SQL Server Native Client 11.0};'
'SERVER=server;'
'Integrated_Security=false;'
'Trusted_Connection=no;'
'UID=pi;'
'PWD=pi;'
'DATABASE= database'
)
cursor = conn.cursor()
cursor.execute('SELECT * FROM database.table')
for row in cursor:
print(row)
yodbc.InterfaceError: ('28000', '[28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user \'pi\'. (18456) (SQLDriverConnect); [28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database " database" requested by the login. The login failed. (4060); [28000] [Microsoft][SQL Server Native Client 11.0]Invalid connection string attribute (0); [28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user \'pi\'. (18456); [28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database " database" requested by the login. The login failed. (4060); [28000] [Microsoft][SQL Server Native Client 11.0]Invalid connection string attribute (0)')
我强烈推荐使用 sqlite3
import sqlite3
conn = sqlite3.connect(
'DRIVER={SQL Server Native Client 11.0};'
'SERVER=server;'
'Integrated_Security=false;'
'Trusted_Connection=no;'
'UID=pi;'
'PWD=pi;'
'DATABASE= database'
)
cursor = conn.cursor()
cursor.execute('SELECT * FROM database.table')
for row in cursor:
print(row)
所以一位同事刚从我身边经过,告诉我将所有数据放在 '' 之间,仅在 1 行中 我还删除了'Integrated_Security=false;'Trusted_Connection=no;有人告诉我
工作代码
something='something'
import pyodbc
conn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER=srv;DATABASE=datavase;UID=pi;PWD=pass')
cursor = conn.cursor()
cursor.execute("insert into test values (?, 'awesome library ')",var)
cursor.execute('SELECT * FROM test')
for row in cursor:
print(row)