Create SQLite Tables

Creating desktop or client-server database solutions?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
SChamblee
Posts: 19
Joined: Wed Jun 24, 2015 7:45 pm

Create SQLite Tables

Post by SChamblee » Mon Aug 24, 2015 4:25 pm

LiveCode 7.0.6

I can create 1 table in a SQLite Database but I can't seem to get LC to create a second table in the same databases.
I have tried the following with the create table combined and I have also use two separate lines and neither has succeeded in creating the images table. The Inventory table is created but not the second table.

put getDatabaseID() into tDatabaseID
put "CREATE TABLE Inventory(Description char(360), DatePurchased Date(10) , PurchasePrice Integer(10),Id INTEGER PRIMARY KEY AUTOINCREMENT); CREATE TABLE Images(Id INTEGER PRIMARY KEY AUTOINCREMENT, Image BLOB , InventoryId INTEGER FOREIGN KEY REFERENCES INVENTORY(Id))" into tSQL
revExecuteSQL tDatabaseID, tSQL


Thanks Stacy

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am
Location: Bordeaux, France

Re: Create SQLite Tables

Post by Dixie » Mon Aug 24, 2015 6:37 pm

Mmm... I create each table one at a time, for example...

Code: Select all

    /* if the database does not exist then create it and build the tables */
      put revOpenDatabase("sqlite", "ptlsdb.sqlite", "binary") into connID
      
      put "CREATE TABLE events(id integer primary key autoincrement not null,icu_id integer, nrd_original integer," & \
            " nrd_adjusted integer, actual integer,type text, duration integer, result text,icu_level integer)" into tSQL
      revExecuteSQL connID, tSQL
      put "CREATE TABLE icus(id integer primary key autoincrement not null,source_id integer, question text, clue text," & \
            " answer text,tags text, level integer, nrd_original integer, nrd_adjusted integer,archived text, deferred text)" into tSQL
      revExecuteSQL connID, tSQL

Klaus
Posts: 13845
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Create SQLite Tables

Post by Klaus » Mon Aug 24, 2015 6:55 pm

Yep, looks like we cannot put 2 "create table" commands in 1 sql query in Livecode/SQLite!?
Could be worse! :D

SChamblee
Posts: 19
Joined: Wed Jun 24, 2015 7:45 pm

Re: Create SQLite Tables

Post by SChamblee » Mon Aug 24, 2015 7:12 pm

Klaus

I had tested separate sql statement and didn't have any luck, so I will have to take another look at my sql statements.

I give your code a test too, maybe I can see what I did wrong.

Thanks for the help

Stacy

SChamblee
Posts: 19
Joined: Wed Jun 24, 2015 7:45 pm

Re: Create SQLite Tables

Post by SChamblee » Mon Aug 24, 2015 8:51 pm

The issue that causes the table not to be created is the Foreign Key Reference statement. https://www.sqlite.org/foreignkeys.html

I assume this is not supported in LC.

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Create SQLite Tables

Post by MaxV » Fri Aug 28, 2015 2:17 pm

SQlite doesn't support foreign keys (they are evil), however you can activate them each time you start a connection:

PRAGMA foreign_keys; test if foreign keys are active: 0 = no, 1=yes

PRAGMA foreign_keys = ON; activate foreign keys

Note:

If the command "PRAGMA foreign_keys" returns no data instead of a single row containing "0" or "1", then the version of SQLite you are using does not support foreign keys at all.
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Post Reply

Return to “Databases”