简体   繁体   English

使用python将变量数据插入SQL Server

[英]Insert variable Data into SQL server using python

def LiraRateApiCall():
R = requests.get(url)
timestamp = R.json()['buy'][-1][0]/1000
format_date = '%d/%m/%y'
date = datetime.fromtimestamp(timestamp)
buyRate = R.json()['buy'][-1][1]
print(date.strftime(format_date))
print(buyRate)


#ADDDING TO SQL SERVER
conn = odbc.connect("Driver={ODBC Driver 17 for SQL Server};"
                    'Server=LAPTOP-36NUUO53\SQLEXPRESS;'
                    'Database=test;'
                    'Trusted_connection=yes;')
cursor = conn.cursor()
cursor.execute('''
                INSERT INTO Data_table (Time1,Price)
                VALUES
                ('date',140),
                ('Date2' , 142)
                ''')
conn.commit()
cursor.execute('SELECT * FROM Data_table')

for i in cursor:
    print(i)

How do i pass the variables date and buy rate to the table instead of putting in values liek i did (i put in'date' , 140 for example but i want to pass variables not specific values)我如何将变量 date 和 buy rate 传递到表中,而不是输入我所做的值(我输入'date',例如 140,但我想传递变量而不是特定值)

You'll need to check the driver version that you're using, but what you're looking for is the concept of bind variables.您需要检查您正在使用的驱动程序版本,但您正在寻找的是绑定变量的概念。 I'd suggest you look into the concept of fast_executemany as well - that should help speed things up.我建议你也研究一下 fast_executemany 的概念——这应该有助于加快速度。 I've edited your code to show how bind variables typically work (using the (?, ?) SQL syntax), but there are other formats out there.我已经编辑了您的代码以显示绑定变量通常如何工作(使用 (?, ?) SQL 语法),但还有其他格式。

def LiraRateApiCall():
R = requests.get(url)
timestamp = R.json()['buy'][-1][0]/1000
format_date = '%d/%m/%y'
date = datetime.fromtimestamp(timestamp)
buyRate = R.json()['buy'][-1][1]
print(date.strftime(format_date))
print(buyRate)


#ADDDING TO SQL SERVER
conn = odbc.connect("Driver={ODBC Driver 17 for SQL Server};"
                    'Server=LAPTOP-36NUUO53\SQLEXPRESS;'
                    'Database=test;'
                    'Trusted_connection=yes;')
cursor = conn.cursor()

#Setup data
data = [('date',140), ('Date2' , 142)]

#Use executemany since we have a list
cursor.executemany('''
                INSERT INTO Data_table (Time1,Price)
                VALUES (?, ?)
                ''', data)

conn.commit()
cursor.execute('SELECT * FROM Data_table')

for i in cursor:
    print(i)

I dont understand at all your question我完全不明白你的问题

If you want to pass the variables:如果要传递变量:

insert_sql = 'INSERT INTO Data_table (Time1,Price) VALUES (' + date + ',' + str(buyRate) + ')'
cursor.execute(insert_sql)

If you want to do dynamic Insert:如果你想做动态插入:

You can only insert knowing the values ​​or by inserting with a select您只能插入知道值或通过选择插入

INSERT INTO table
SELECT * FROM tableAux
WHERE condition;

That or you could iterate through the fields you have in a table, extract them and compare it to your variables to do a dynamic insert.那或者您可以遍历表中的字段,提取它们并将其与变量进行比较以进行动态插入。

With this select you can extract the columns.使用此选择,您可以提取列。

SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'table1'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM