473,388 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

pysqlite - Checking the existance of a table

Hi all,

I am starting to play with pysqlite, and would like to know if there is
a function to determine if a table exists or not.
Thanks

Jul 19 '05 #1
4 1959
Simply use the internal table SQLite_Master:

select name from SQLite_Master

will return all existing tables.

Regards,
Matthias

Jul 19 '05 #2
| I am starting to play with pysqlite,
| and would like to know if there is a function
| to determine if a table exists or not.

rh0dium ....

One way to get at a list of table names
in an SQLite data base is to query
the sqlite_master table ....
import sys
import sqlite

this_db = sys.argv[ 1 ]

list_sql = [ "select tbl_name" ,
"from sqlite_master" ]

str_sql = '\n'.join( list_sql )

dbc = sqlite.connect( db = "%s" % this_db )

curs = dbc.cursor()

curs.execute( str_sql )

list_tables = curs.fetchall()

print '\n Table Names in SQLite DB .... %s \n' % ( this_db )

for table_name in list_tables :

print " %s " % ( table_name )

print

dbc.close()
--
Stanley C. Kitching
Human Being
Phoenix, Arizona

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 19 '05 #3
rh0dium wrote:
Hi all,

I am starting to play with pysqlite, and would like to know if there is
a function to determine if a table exists or not.


You can try to access the table in a try-catch block, something like:

cur.execute("select * from tablename where 1=2")

and check if it fails.

Or you can query the sqlite_master table (don't know any specification
off-hand, but it contains the schema information).

Instead of doing a select on sqlite_master, you can use "pragma
table_info", which returns information for each column in the table,
and, apparently, an empty list if the table does not exist:
cur.execute("pragma table_info(foo)")
print cur.fetchall() [(0, u'bar', u'integer', 0, None, 0)]
cur.execute("pragma table_info(foo_does_not_exist)")
print cur.fetchall()

[]

HTH,

-- Gerhard
Jul 19 '05 #4
Gerhard Häring wrote:
Or you can query the sqlite_master table (don't know any specification
off-hand, but it contains the schema information).


Item #9 in the FAQ (http://www.sqlite.org/faq.html#q9) shows it as:

CREATE TABLE sqlite_master (
type TEXT,
name TEXT,
tbl_name TEXT,
rootpage INTEGER,
sql TEXT
);
-Peter
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Irmen de Jong | last post by:
Hello I'm having some troubles with PySqlite (0.4.3, windows/linux). I want to store unicode strings in the database. So I open a connection with the client_encoding="UTF-8" parameter. When I...
2
by: William Kossack | last post by:
I'm trying to write a program in cold fusion to check the existance of fields and data types according to requirements I was looking at the syscolumns table for some of this information but I've...
1
by: DurumDara | last post by:
Hi ! I have this code in my program. Before this I use APSW, but that project's connection object doesn't have close method... .... crs.execute(*'''create table files (*f_id integer not null...
1
by: Thomas | last post by:
Hi there! Installing TurboGears out-of-the-box (egg-based) on Windows doesn't work because I can't compile the extensions needed for the required pysqlite (also egg- based): Installed...
14
by: Nader Emami | last post by:
I have installed "TurboGears" and I would install 'pysqlite' also. I am a user on a Linux machine. If I try to install the 'pysqlite' with 'easy_install' tool I get the next error message. The...
0
by: Wildemar Wildenburger | last post by:
Hi there, Using pysqlite, I'd like to check if some dataset that I removed has been in the database at all. Ideally I'd like pysqlite to raise an Exception if deleting does nothing. Is that...
6
by: Andy B | last post by:
How do you check to see if a certain folder exists? I also need to know how to check for the existance of a certain sql server database table.
4
by: Tilman Kispersky | last post by:
I am trying to install sqlite for use with python on cygwin. I have installed the sqlite packages from cygwin (that is libsqlite3-devel and libsqlite3_0). When attempting to easy_install pysqlite...
4
by: Astley Le Jasper | last post by:
I've been getting errors recently when using pysqlite. I've declared the table columns as real numbers to 2 decimal places (I'm dealing with money), but when doing division on two numbers that...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.