Jump to content

SQLite.au3 - how to test if table exsists already.


John117
 Share

Recommended Posts

I have a simple test script for this post.

Trying to get my script to verify if a table exsists, if not, then add the table.

Have tried sqlite statements I have found. none seem to work for me. Anyone here know sqlite enough to make this check if the table exsists before adding it. Just looking to get rid of the error the second time it is run.

It runs in my main script everytime the script loads. -needs to.

#include <SQLite.au3>
#include <SQLite.dll.au3>

_Query_Database_Startup_Info()

Func _Query_Database_Startup_Info()
 Global $SqlLiteDll = _SQLite_Startup()
 If @error Then
  MsgBox(16, "Error", "Failed to start up SQLite, @error = " & @error)
 Else
  $hDB = _SQLite_Open(@ScriptDir & "\CIF1.db")
  If $hDB = 0 Or @error Then
   MsgBox(16, "Error", "Error opening database, @error = " & @error & ", @extended = " & @extended)
  Else
   $sExec = "CREATE TABLE TableNameHere (ID_Here INTEGER PRIMARY KEY,a,b,c,d,e);"
   _SQLite_Exec($hDB, $sExec)
   _SQLite_Close($hDB)
   _SQLite_Shutdown()
  EndIf
 EndIf
EndFunc   ;==>_Query_Database_Startup_Info
Link to comment
Share on other sites

select count(*) from sqlite_master where type='table' and name like 'mytable'

will return 0 or 1 depending on table existence.

Now the kludge above is probably not what you actually need. Try this instead:

$sExec = "CREATE TABLE if not exists TableNameHere (ID_Here INTEGER PRIMARY KEY,a,b,c,d,e);"

Pure SQL does it. See SQLite docs.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Pretty standard way to [re]create tables in SQL without having to worry about what happened just before.

BTW, try avoiding declaring columns without a type, even given that SQLite is quite permissive about datatypes. Or rather declare intended types _because_ SQLite is quite smart by its own and will do its best to fulfill your desires.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...