Python Forum
sqlite3 table structure and db tables
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sqlite3 table structure and db tables
#1
I'm working on a small program to keep track of library books and I'm doing the program in python3 and sqlite3. I'm not very far into the program at all and at the moment I just need to validate my sqlite3 table creation code is working. How can can I see a list of table for my sqlite3 database using python3, and I'd like to be able to see that table sctructure as well.

main.py
import db

db.setup_db()

db.conn

input("Press any key to continue")
db.py
import sqlite3
import os


def setup_db():
    directory = "C:\\Users\\me\\librarydb"

    if not os.path.exists(directory):
        print("directory does not exist")
        os.mkdir(directory)
        #conn = sqlite3.connect(r"C:\\Users\\me\\librarydb\\booksdb")
        build_db()

    else:
        print("directory exists")
        dbfile = "C:\\Users\\me\\librarydb\\booksdb"
        if not os.path.exists(dbfile):
            #conn = sqlite3.connect(r"C:\\Users\\me\\librarydb\\booksdb")
            build_db()

def build_db():
    conn = sqlite3.connect(r"C:\\Users\\me\\librarydb\\booksdb")
    c = conn.cursor()
    c.execute('''CREATE TABLE Books
                (book_id int,
                author_id int,
                book_title varchar(100),
                genre varchar(50),
                type varchar(30),
                cover char,
                price real)''')

    conn.commit()
    conn.close()
Reply
#2
"conn=" should return an error if the table does not exist. Note that both conn and c (cursor) are garbage collected when the function exits so return them if you want to use them elsewhere http://www.tutorialspoint.com/python/pyt...ctions.htm
To print the table structure use PRAGMA
import sqlite3 as sqlite

table_name="C:/Users/me/librarydb/booksdb"
con = sqlite.connect(table_name)
cur = con.cursor()

meta = cur.execute("PRAGMA table_info(%s)" % (table_name))
for r in meta:
    print(r) 
Reply
#3
The query:
select name from sqlite_master where type = 'table';
will shoq LL Tables
Reply
#4
(May-15-2018, 06:43 PM)woooee Wrote: "conn=" should return an error if the table does not exist. Note that both conn and c (cursor) are garbage collected when the function exits so return them if you want to use them elsewhere http://www.tutorialspoint.com/python/pyt...ctions.htm
To print the table structure use PRAGMA
import sqlite3 as sqlite

table_name="C:/Users/me/librarydb/booksdb"
con = sqlite.connect(table_name)
cur = con.cursor()

meta = cur.execute("PRAGMA table_info(%s)" % (table_name))
for r in meta:
    print(r) 

I thought conn = would open the database sqlite file if it exist or create the file if it doesn't?
Reply
#5
slight error on database open:
open database with:
con = sqlite3.connect(table_name)
To get list of tables add following on line 7:
def show_tables(cur):
    sqlstr = "select name from sqlite_master where type = 'table';"
    tables = cur.execute(sqlstr)
    for row in tables:
        print(row)
show_tables(cur)
Reply
#6
Quote:I thought conn = would open the database sqlite file if it exist or create the file if it doesn't?
I was referring to a table, not a file, my mistake. To create a table if it doesn't exist, use
cur.execute("CREATE TABLE IF NOT EXISTS " etc. 

Quote:slight error on database open:
open database with:
con = sqlite3.connect(table_name)
I import sqliteX as sqlite. Then, if there is a version change I only have to update the import statement.
Reply
#7
Quote:I import sqliteX as sqlite. Then, if there is a version change I only have to update the import statement.
IP Address: Logged
OK, that's fine.
This function will still show all tables
Reply
#8
SOLVED
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Using SQLAlchemy, prevent SQLite3 table update by multiple program instances Calab 3 765 Aug-09-2023, 05:51 PM
Last Post: Calab
  Slittping table into Multiple tables by rows drunkenneo 1 2,071 Oct-06-2021, 03:17 PM
Last Post: snippsat
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 7,815 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt
  Python script to summarize excel tables, then output a composite table? i'm a total n surfer349 1 2,364 Feb-05-2021, 04:37 PM
Last Post: nilamo
  Load data from One oracle Table to Multiple tables amy83 1 1,792 Dec-02-2020, 01:57 AM
Last Post: Larz60+
  Adding data to a table in SQLite3 djwilson0495 2 3,086 Aug-15-2020, 02:48 PM
Last Post: djwilson0495
  Creating a table in SQLite3 djwilson0495 2 2,071 Aug-10-2020, 03:01 PM
Last Post: djwilson0495
  How read Table Structure from Database nnsatpute 5 3,745 Oct-25-2019, 10:08 AM
Last Post: Larz60+
  Database table structure compare(Oracle) GKT 4 2,785 Oct-16-2019, 03:16 PM
Last Post: GKT
  sqlite3 db and table design pythonNoob 4 3,818 May-22-2018, 11:21 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020