Skip to content

Commit d24c8a4

Browse files
committed
Add sqlite sketch with ketai
1 parent 26d5ddb commit d24c8a4

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

ketai_sketches/ketai_sqlite.pde

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import ketai.data.*;
2+
3+
KetaiSQLite db;
4+
String CREATE_DB_SQL = "CREATE TABLE data ( _id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER NOT NULL DEFAULT '0');";
5+
6+
void setup()
7+
{
8+
fullScreen();
9+
db = new KetaiSQLite( this); // open database file
10+
11+
if ( db.connect() )
12+
{
13+
// for initial app launch there are no tables so we make one
14+
if (!db.tableExists("data"))
15+
db.execute(CREATE_DB_SQL);
16+
17+
println("data count for data table: "+db.getRecordCount("data"));
18+
19+
//lets insert a random number or records
20+
int count = (int)random(1, 5);
21+
22+
for (int i=0; i < count; i++)
23+
if (!db.execute("INSERT into data (`name`,`age`) VALUES ('person"+(int)random(0, 100)+"', '"+(int)random(1, 100)+"' )"))
24+
println("error w/sql insert");
25+
26+
println("data count for data table after insert: "+db.getRecordCount("data"));
27+
28+
// read all in table "table_one"
29+
db.query( "SELECT * FROM data" );
30+
31+
while (db.next ())
32+
{
33+
println("----------------");
34+
print( db.getString("name") );
35+
print( "\t"+db.getInt("age") );
36+
println("\t"+db.getInt("foobar")); //doesn't exist we get '0' returned
37+
println("----------------");
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)