Page 1 of 1

SQLite tables not being created

Posted: Fri Feb 04, 2011 11:07 pm
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!

Re: SQLite tables not being created

Posted: Fri Feb 04, 2011 11:40 pm
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

Re: SQLite tables not being created

Posted: Sat Feb 05, 2011 6:04 am
by tutiplain
Right, I see it. Thanks, I'll try that tomorrow.

Re: SQLite tables not being created

Posted: Sat Feb 05, 2011 11:30 pm
by Kiffi
ABBKlaus wrote:Your createfile() misses a closefile
and, @tutiplain: Use DatabaseUpdate() instead of DatabaseQuery()

Greetings ... Kiffi

Re: SQLite tables not being created

Posted: Mon Feb 07, 2011 7:41 pm
by ABBKlaus
and last but not least use CloseDataBase() :twisted:

Re: SQLite tables not being created

Posted: Mon Feb 07, 2011 10:22 pm
by Kiffi
ABBKlaus wrote:and last but not least use CloseDataBase() :twisted:
and -- of course -- FinishDatabaseQuery() is not necessary anymore :mrgreen: