SQLite tables not being created

Just starting out? Need help? Post your questions and find answers here.
tutiplain
User
User
Posts: 43
Joined: Wed Jun 30, 2010 3:00 am

SQLite tables not being created

Post by tutiplain »

Hi, I'm doing a simple app which reads from a Sqlite database. I'd like the program to be able to create the database file on its own if it doesn't exist, like a first-time setup. While the database file gets created with my code, the sql CREATE command for my table fails. Here's my code:

Code: Select all

Procedure CreateData()
  
  db_handle = CreateFile(#PB_Any,"data.dat")
  db_handle = OpenDatabase(#PB_Any,"data.dat","","",#PB_Database_SQLite)
  If db_handle=0
    MessageRequester("Fatal Error","Fatal Error. Database cannot be created.")
    End 
  Else
    stmt$="CREATE TABLE Alert (id INTEGER PRIMARY KEY  AUTOINCREMENT  Not NULL , description VARCHAR, alert_time DATETIME)"
    r= DatabaseQuery(db_handle,stmt$)
    If r=0
      MessageRequester("Fatal Error","Unable to Initialize Data Tables.")
      End
    Else
      MessageRequester("Success!","Database succesfully initialized for first run.")
    EndIf
    FinishDatabaseQuery(db_handle)
  EndIf
EndProcedure    
While not included Here, I called UseSQLiteDatabase() in a procedure which executes before this. The value 'r' returned from DatabaseQuery() is equal to 1, but the data file measures 0kb on disk, and attempting to do an INSERT command returns a "table does not exist" error. Can anyone help? Thanks!
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Re: SQLite tables not being created

Post by ABBKlaus »

Your createfile() misses a closefile :wink:

Please look at the example for UseSQLDataBase() here : http://www.purebasic.com/documentation/ ... abase.html

BR Klaus
tutiplain
User
User
Posts: 43
Joined: Wed Jun 30, 2010 3:00 am

Re: SQLite tables not being created

Post by tutiplain »

Right, I see it. Thanks, I'll try that tomorrow.
User avatar
Kiffi
Addict
Addict
Posts: 1520
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: SQLite tables not being created

Post by Kiffi »

ABBKlaus wrote:Your createfile() misses a closefile
and, @tutiplain: Use DatabaseUpdate() instead of DatabaseQuery()

Greetings ... Kiffi
Hygge
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Re: SQLite tables not being created

Post by ABBKlaus »

and last but not least use CloseDataBase() :twisted:
User avatar
Kiffi
Addict
Addict
Posts: 1520
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: SQLite tables not being created

Post by Kiffi »

ABBKlaus wrote:and last but not least use CloseDataBase() :twisted:
and -- of course -- FinishDatabaseQuery() is not necessary anymore :mrgreen:
Hygge
Post Reply