473,379 Members | 1,190 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,379 software developers and data experts.

SQLite Create Table

158 100+
What i need to do is create a table and add the columns but if it already exists i do not want to create it because i get errors because there is already that table. How would i like check if the table already exists?

my code so far looks like this.

Expand|Select|Wrap|Line Numbers
  1. use DBI;
  2.  
  3. my $dbh = DBI->connect( "dbi:SQLite:../data/db/lightmon.db" ) || die "Cannot connect: $DBI::errstr";
  4.    $dbh->do( "CREATE TABLE 'Node$node' ( id, notify_date, msg_id, msg, notes )" );
Jun 25 '07 #1
1 5504
miller
1,089 Expert 1GB
Something like the following can help. Just note that it is subject to a race condition, so the easier route would probably be to just capture the failed sql query.

Expand|Select|Wrap|Line Numbers
  1. # Determine if table exists
  2. sub isTable {
  3.     my $tableName = shift;
  4.  
  5.     my %tables = ();
  6.  
  7.     my $name;
  8.     my $sth = $dbh->prepare(q{SHOW TABLES});
  9.     $sth->execute or die $dbh->errstr;
  10.     $sth->bind_columns(\$name);
  11.     while ($sth->fetch) {
  12.         $tables{$name} = 1 
  13.     }
  14.     $sth->finish; undef $sth;
  15.  
  16.     return $tables{$tableName};
  17. }
  18.  
- Miller
Jun 25 '07 #2

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

Similar topics

3
by: firephreek | last post by:
Is there some documentation out there that actually lists the Sqlite syntax? Or even better, one that crosses over and talks about how it works with python? Sqlite seems quite the nifty little...
2
by: Christian Stooker | last post by:
Part one: ====== Hi ! I want to use SQLite database like the FireBird database: with big isolation level. What's that meaning ? I have an application that periodically check some input...
1
by: jander22 | last post by:
Hi, I just started playing with Python and I am working on a program that pulls data from a table in Oracle and creates a similar table in SQLite. Basically, I want to be able to do this: create...
4
by: Jim Carlock | last post by:
I added the following lines to PHP.INI. extension=php_pdo.dll extension=php_pdo_sqlite.dll extension=php_sqlite.dll specifically in that order. I noticed the extensions getting loaded are...
0
by: smitty1e | last post by:
Disclaimer(s): the author is nobody's pythonista. This could probably be done more elegantly. The driver for the effort is to get PyMacs to work with new-style classes. This rendering stage...
6
by: Rob Stevens | last post by:
I have been trying to find some working examples of working with this DB, but I can't seem to find anything at all. If I do find examples, its all referring to the command line program that...
3
by: kyosohma | last post by:
Hi, I am trying to use sqlite to create a local database for an application I am writing and I am getting some screwy results from it. Basically, I have a set of values in the database and I am...
0
by: Joe Goldthwaite | last post by:
Thanks Guilherme. That helped. I guess I was thinking that pysqlite would automatically come with some version of sqlite. The fact that it doesn't is what was causing me to get the strange...
4
by: marc wyburn | last post by:
Hi I'm using SQlite and the CSV module and trying to create a class that converts data from CSV file into a SQLite table. My script curently uses functions for everything and I'm trying to...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?

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.