Python / Basics / Sqlite DB
SQLITE DB
-
Step:
import sqlite3 from sqlite3 import Error import pandas as pd def create_connection(db_file): """ create a database connection to a SQLite database """ conn = None try: conn = sqlite3.connect(db_file) print(sqlite3.version) cur = conn.cursor() cur.execute(''' CREATE TABLE contact_list( id integer PRIMARY KEY, name VARCHAR, mobile VARCHAR )''') except Error as e: print(e) finally: dataframe1 = pd.read_excel('contact_list.xlsx') for index, row in dataframe1.iterrows(): print (index,row) sqlite_insert_with_param = """INSERT INTO contact_list (id, name, mobile) VALUES (?, ?, ?);""" data_tuple = (0, row[0], row[1]) cur.execute(sqlite_insert_with_param, data_tuple) conn.commit() cur.execute("SELECT * FROM contact_list") # storing the data in a list data_list = cur.fetchall() print(data_list) for item in data_list: print(item) print('Roll_Number' + '\t Name') if conn: conn.close() if __name__ == '__main__': create_connection(r"E:\pythonsqlite.db")