I am new to TideSDK and am tring to get the local database working.
First of all, is this the right thing to use? I expect the user will load the game, put some stuff (the local database) in without sending to the server, quit the game. Then come back to it later (even after turning off his pc or clearing his internet), load the game and all the local database stuff is still there, and then he can send it to the server then. (Should I be using a local file instead?)
The error I am getting is with:
db.execute("INSERT INTO Games(id, player, timeText) VALUES(1, 1, 0)"); // Putting a zero time in
Its saying it is an incorrect sql statement or database does not exist.
Ideally, I would also like to do the sqlite with prepared statements, but I wanted to start simpler.
This is the mod of the HelloWorld example:
https://github.com/TideSDK/TideSDK-HelloWorld
My modded code:
app.js
function DoSqlCall() {
var lastTime;
//Open the database first
var db = Ti.Database.openFile(Ti.Filesystem.getFile(
Ti.Filesystem.getApplicationDataDirectory(), 'customdatabase.db'));
//Create a table and insert values into it
db.execute("CREATE TABLE IF NOT EXISTS Games(id INTEGER, player INTEGER, timeText TEXT)");
alert("Hello 1");
var rows = db.execute("SELECT * FROM Games WHERE 'id' = 1 AND 'player' = 1");
if(rows.isValidRow()) {
lastTime = rows.fieldByName('timeText');
alert("lastTime: " + lastTime.toString());
//rows.next();
} else {
db.execute("INSERT INTO Games(id, player, timeText) VALUES(1, 1, 0)"); // Putting a zero time in
alert("Put in zero time");
lastTime = 0;
}
//Release memory once you are done with the resultset and the database
rows.close();
db.close();
};
// create and set menu
var menu = Ti.UI.createMenu(),
fileItem = Ti.UI.createMenuItem('File'),
exitItem = fileItem.addItem('Exit', function() {
if (confirm('Are you sure you want to quit?')) {
Ti.App.exit();
}
});
menu.appendItem(fileItem);
Ti.UI.setMenu(menu);
index.html
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<style type="text/css">
body {background: #fff;}
</style>
</head>
<body>
<h1>Hello World</h1>
<button id="m_Send" type="button" onclick="DoSqlCall()">Send</button>
<script type="text/javascript" src="app.js"></script>
</body>
</html>
In your SQL statement you have this:
db.execute("INSERT INTO Games(id, player, timeText) VALUES(1, 1, 0)");
Where you're passing an integer for the timeText field, however, you've defined this field as a TEXT field. I suggest you try this:
db.execute("INSERT INTO Games(id, player, timeText) VALUES(1, 1, '0')");
Here, I'm passing a string for the third field.