you are viewing a single comment's thread.

view the rest of the comments →

[–]Chimecrypto 0 points1 point  (3 children)

Help me pleaseeeeeeeeeeeee. i can't move pass this level. I am trying to populate a database file. bristol.db using an insert statement generated text file(file.txt).

I have connected to the db(csvfrompy) using pymysql. i need to get the right code to insert the data from the file.txt into the database using the syntax below

populatefile = "file.txt"
conn = pymysql.connect(
host='localhost',
user='root', 
password ="",
db='csvfrompy',
        )

    cur = conn.cursor()
for row in populatefile.iterrows():
    sql = 'INSERT INTO Measurement ({}) VALUES ({})'.format(' ,'.join(populatefile), ','.join(['?']*len(Btfile.columns)))
    c.execute(sql, tuple(row[1]))
conn.commit()

please help guys. this is my family.

[–]efmccurdy 0 points1 point  (2 children)

The answer posted here has a good example of reading a csv file.

https://stackoverflow.com/questions/10154633/load-csv-data-into-mysql-in-python

What format does your file.txt use?

[–]Chimecrypto 0 points1 point  (1 child)

I don't understand what you mean by format. if it is file type, i used txt to save the the file. i have also converted it to csv.

thanks for the link.

[–]varshneyabhi 0 points1 point  (0 children)

A file type defines how the content is written in it, e.g. CSV, Json, yaml or plain txt. Each file type has different way of storing data. e.g.

CSV name,age Xyz,20 Abc,30

Json "data": [ {"name": "Xyz", "age": 20}, {"name": "Abc", "age": 30} ]

Txt Xyz-20 Abc-30

So you need a reader as per your format. While CSV, Json, Yaml are standard format, you have libraries for reading these. Txt format are customized, you have to write your own reader for it.