Importing text file data to sqlite3

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ryno du preez
    New Member
    • Jul 2011
    • 91

    Importing text file data to sqlite3

    Hi all I'm new to Python (just finnished training books)
    I have a txt file with lines of data No Make and price
    exp. (1 BMW 20000).
    Now i want to import or copy this data to sqlite database.
    I can get python to read the data and display it but doesn't copy it accross and also there is know errors.

    Code:
    import os
    import sys
    import sqlite3 as lite
    
    #*car = open('C:/Users/rynod/Desktop/car.txt','r')
    #car2 = car.read()
    
    
    for car in open('C:/Users/rynod/Desktop/car.txt'):
          car2 = car.split()
          count = 0
          
          for i in (car2):
              if '1' in i:
                  print(car2)
                  
                
                  
     
    #Create Database
    con = lite.connect('C:/db/test.db')
    
    with con:
    
        cur = con.cursor()
     #   cur.execute("INSERT INTO Cars VALUES(1,'Audi',57127)")
        cur.execute("DROP TABLE IF EXISTS Cars")
        cur.execute("CREATE TABLE Cars(Id INT, Name TEXT, Price INT)")
               
        cur.executemany("INSERT INTO Cars(Id, Name, Price) Values(?, ?, ?);",car2)
        con.commit()
Working...