[英]Writing Data in Sql table from a text file using Pandas Module
I was trying to read some data from a text file and write it down in a Sql server table using Pandas Module and FOR LOOP.我试图从文本文件中读取一些数据,并使用 Pandas 模块和 FOR LOOP 将其写入 Sql 服务器表中。 Below is my code..
下面是我的代码..
import pandas as pd
import pyodbc
driver = '{SQL Server Native Client 11.0}'
conn = pyodbc.connect(
Trusted_Connection = 'Yes',
Driver = driver,
Server = '***********',
Database = 'Sullins_Data'
)
def createdata():
cursor = conn.cursor()
cursor.execute(
'insert into Sullins_Datasheet(Part_Number,Web_Link) values(?,?);',
(a,j))
conn.commit()
a = pd.read_csv('check9.txt',header=None, names=['Part_Number','Web_Links'] ) # 2 Columns, 8 rows
b = pd.DataFrame(a)
p_no = (b['Part_Number'])
w_link = (b['Web_Links'])
# print(p_no)
for i in p_no:
a = i
for l in w_link:
j = l
createdata()
As you can see from the code that I have created 2 variables a and j to hold the value of both the columns of the text file one by one and write it in the sql table.从代码中可以看出,我创建了 2 个变量 a 和 j 来一一保存文本文件两列的值,并将其写入 sql 表中。 But after running the code I have got only the last row value in the table out of 8 rows.
但是在运行代码之后,我只得到了表中 8 行中的最后一行值。 When I used createdate function inside w_link for loop, it write the duplicate value in the table.
当我在 w_link for 循环中使用 createate function 时,它会将重复值写入表中。 Please suggest where I am doing wrong.
请建议我在哪里做错了。
here is sample of how your code is working这是您的代码如何工作的示例
a = 0
b = 0
ptr=['s','d','f','e']
pt=['a','b','c','d']
for i in ptr:
a=i
print(a,end='')
for j in pt:
b=j
print(b,end='')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.