Recent

Author Topic: [SOLVED]error populating SQLite Database  (Read 6103 times)

hy

  • Full Member
  • ***
  • Posts: 224
[SOLVED]error populating SQLite Database
« on: October 07, 2014, 08:07:24 pm »
Hi I creating an SQLite Database with the following code:

   
Code: [Select]
sqlite3conn.connected := false;
   SQLite3Conn.DatabaseName := '/media/work/testarea/ymap.db';
   qrySQLiteMain.SQL.Clear;
   qrySQLiteMain.SQL.add('Create table SD (id integer,iX integer,iY integer)');
   SQLite3conn.connected := True;
   qrySQLiteMain.ExecSQL;   //created

   qrySQLiteMain.SQL.Clear;
   qrySQLiteMain.SQL.add('insert into SD VALUES (1,8,9);');
   qrySQLiteMain.ExecSQL;

   qrySQLiteMain.SQL.Clear;
   qrySQLiteMain.SQL.add('insert into SD VALUES (2,18,29);');
   qrySQLiteMain.ExecSQL;

   qrySQLiteMain.SQL.Clear;
   qrySQLiteMain.SQL.add('insert into SD VALUES (3,18,19);');
   qrySQLiteMain.ExecSQL;

   SQLite3conn.connected := FALSE;
   
The file is created with the size of 0 - so no data is stored at all... what am I doing wrong?
Thanks in advance

EDIT: When using
Code: [Select]
SQLite3conn.CreateDB;I get a runtime-error:
Code: [Select]
EDatabaseError: Operation is not supported by this type of database
« Last Edit: October 07, 2014, 09:19:26 pm by hy »
_____
***hy

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1263
Re: error populating SQLite Database
« Reply #1 on: October 07, 2014, 08:57:31 pm »
Well, according to
http://wiki.lazarus.freepascal.org/SQLite

CreateDB is the correct way to go.

Where in your code do you insert the .CreateDB line?

Ug.  The following page confirms your exception, which tells me that TSQLiteConnection does not support CreateDB, which contradicts the wiki...

http://www.freepascal.org/docs-html/fcl/sqldb/tsqlconnection.createdb.html

Look, I'm going to believe the wiki here.  Something else must be causing that error.

UPDATE:  I've now reversed my position after a mere 5 minutes searching these forums.  Everything you're doing is apparently correct.  I've no idea :-(
« Last Edit: October 07, 2014, 09:08:00 pm by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

hy

  • Full Member
  • ***
  • Posts: 224
Re: error populating SQLite Database
« Reply #2 on: October 07, 2014, 09:00:12 pm »
I inserted
SQLite3conn.CreateDB;
after SQLite3conn.connected:=true;
_____
***hy

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1263
Re: error populating SQLite Database
« Reply #3 on: October 07, 2014, 09:08:48 pm »
Yeah, see my update above.  5 minutes searching on these forums, and it looks like your code is correct.

I'll keep searching...

Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

hy

  • Full Member
  • ***
  • Posts: 224
Re: error populating SQLite Database
« Reply #4 on: October 07, 2014, 09:12:46 pm »
Yeah, see my update above.  5 minutes searching on these forums, and it looks like your code is correct.

I'll keep searching...
I'm searching for 4 hours now... )-;
_____
***hy

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1263
Re: error populating SQLite Database
« Reply #5 on: October 07, 2014, 09:15:03 pm »
Aha!  I was looking in the wrong place.

Lazarus - Project - Close Project
Choose View Example Projects
Filter for SQLite, there's only 1 - don't worry that it's an encryption demo, we'll ignore that side
There's a button on there "Create DB". In the code, there's two lines different to yours.
After .Connected := True stick
Code: [Select]
SQLTransaction1.StartTransaction;
after all your .ExecSQL's and before your .Connected := False; stick
Code: [Select]
SQLTransaction1.Commit;
(Or whatever your transaction is named).

Can't believe I fell for this two days in a row...

Basically, everything that happens in SQLDB code happens inside a Transaction (so really, starting the transaction is optional, but good practise).  If you don't Commit the transaction, nothing gets saved...   The permanent transaction only came in a few releases ago, which is why all the historical examples on the forum don't mention it...
« Last Edit: October 07, 2014, 09:18:13 pm by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

hy

  • Full Member
  • ***
  • Posts: 224
Re: error populating SQLite Database
« Reply #6 on: October 07, 2014, 09:19:13 pm »
Thanks i only inserted
Starttransaction
and
endtransaction

i forgot about the commit... (It's some 10 years that I did serious DB-programming)
Thanks again
_____
***hy

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1263
Re: [SOLVED]error populating SQLite Database
« Reply #7 on: October 07, 2014, 09:33:15 pm »
And all this means I've got a wiki to go update...

Can any SQLite experts confirm if the tip on the wiki is in fact correct?
http://wiki.lazarus.freepascal.org/SQLite#Tips

Quote
Use the TSQLite3Connection.CreateDB procedure to create a new SQLite database.

Is there *some* way that CreateDB would have worked?
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

hy

  • Full Member
  • ***
  • Posts: 224
Re: [SOLVED]error populating SQLite Database
« Reply #8 on: October 07, 2014, 10:15:13 pm »
Is there *some* way that CreateDB would have worked?
I think not, because when using createDB it crashes instantly.
_____
***hy

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1263
Re: [SOLVED]error populating SQLite Database
« Reply #9 on: October 07, 2014, 10:28:08 pm »
 :)  Yeah, but maybe we missed the magic
Code: [Select]
.DoThisBeforeCallingCreateDBToMakeCreateDBMagicallyWork;
I just want some verification before I barge in and edit some text on the wiki on a subject I really don't know anything about :-)
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

hy

  • Full Member
  • ***
  • Posts: 224
Re: [SOLVED]error populating SQLite Database
« Reply #10 on: October 07, 2014, 11:09:41 pm »
Sorry, I don't get it.
What should I do?
DoThisBeforeCallingCreateDBToMakeCreateDBMagicallyWork
doesn't actually exist in the internet (aside from some biblical sites) - nor does it in the source...
_____
***hy

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1263
Re: [SOLVED]error populating SQLite Database
« Reply #11 on: October 07, 2014, 11:52:00 pm »
Sorry @Hy, you don't need to do anything - we've got your code working. 

Sorry for spamming this topic - your issue has made me think there is something wrong in the wiki, and I'd like one of the SQLite experts to confirm before I fix it.  I should have started a new topic.

To be clear, I'm not asking you for any clarification, but I agree, using your topic to try and grab the attention of others was confusing.
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

 

TinyPortal © 2005-2018