By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,737 Members | 1,310 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,737 IT Pros & Developers. It's quick & easy.

Importing text file data to sqlite3

ryno du preez
P: 91
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.

Expand|Select|Wrap|Line Numbers
  1. import os
  2. import sys
  3. import sqlite3 as lite
  4.  
  5. #*car = open('C:/Users/rynod/Desktop/car.txt','r')
  6. #car2 = car.read()
  7.  
  8.  
  9. for car in open('C:/Users/rynod/Desktop/car.txt'):
  10.       car2 = car.split()
  11.       count = 0
  12.  
  13.       for i in (car2):
  14.           if '1' in i:
  15.               print(car2)
  16.  
  17.  
  18.  
  19.  
  20. #Create Database
  21. con = lite.connect('C:/db/test.db')
  22.  
  23. with con:
  24.  
  25.     cur = con.cursor()
  26.  #   cur.execute("INSERT INTO Cars VALUES(1,'Audi',57127)")
  27.     cur.execute("DROP TABLE IF EXISTS Cars")
  28.     cur.execute("CREATE TABLE Cars(Id INT, Name TEXT, Price INT)")
  29.  
  30.     cur.executemany("INSERT INTO Cars(Id, Name, Price) Values(?, ?, ?);",car2)
  31.     con.commit()
  32.  
Oct 2 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.