I am experimenting with KMM but I have an issue with SQLDelight. Here is the code sample of the AppDatabase.sq file.
CREATE TABLE Data (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
content TEXT NOT NULL
);
-- ====>> Add this new table <<==== (QUESTION 1)
CREATE TABLE Settings (
version TEXT NOT NULL
);
insertItem:
INSERT INTO Data(id, content) VALUES(?,?);
removeAll:
DELETE FROM Data;
selectById:
SELECT * FROM Data WHERE id = ?;
updateById:
UPDATE Data SET content = ? WHERE id = ?;
-- ====>> I want this to be run on DB creation <<==== (QUESTION 2)
INSERT INTO Settings(version) VALUES ('1.0.0');
getVersion:
SELECT version FROM Settings;
When I run the application I receive:
android.database.sqlite.SQLiteException: no such table: Settings (code 1 SQLITE_ERROR): , while compiling: SELECT version FROM Settings.
If I open the databases created by the app using the App Inspection of Android Studio it seems that only Data table exist and other autogenerated tables which have nothing to do with my tables.
My 2 questions (see comments in SQL code for number reference) are:
How can I create this table (Settings) from .sq file?
How can insert a new value into my table without call any function from actual code?
In case you need the whole project is here but in a previous version because the changes I want to implement are not working!
I have cloned your Github project, added the code you provided here and it works just fine. Both of the tables get created and the Settings table has "1.0.0" version row.
Have you uninstalled the application on the phone and installed it again? It's possible you're still using the old version of the database since you haven't provided any migrations for SQLDelight to do.
Related
i have two tables, an FP_master table and a Take_Away_pack_details table. my first code below successfully copies data to column TakeAwayPkCost in table FP_master from column Pack_Cost in table Take_Away_Pack_Details.
In the second case I have a third table called View_Cart_Temp and I try to copy data from Column TakeAwayPKType in the FP_Master table to column Pack_type in the 3rd table View_Cart_temp. The 2nd piece of SQlite UPDATE code is shown below. The Syntax are identical in both cases and when i run this in my app, there is no error but the column is not updated. When I run this also in the DB Browser for SQLite, I get a message saying successfully updated but when i check the table its still null and no data has been copied. What could I be missing?
UPDATE FP_Master SET TakeAwayPkCost = (SELECT Pack_Cost FROM Take_Away_Pack_Details WHERE Pack_Type = TakeAwayPkType)
UPDATE View_Cart_Temp SET Pack_Type = (SELECT TakeAwayPkType FROM FP_Master WHERE Item = SKU)
I want to alter a table in my android application which is using ORMLite. I will explain my scenario.
Let's say that someone download my app on the Google Play. Few months later I will certainly populate some tables with new entries.
When this person is doing an update of the app, how can I just alter a table in database with my new entries and keep the old ones inside it.
To be more clear, imagine that the user table is altered and i introduced a new field say 'city', how can i update the table with old values?
When i tried to back up the data from older table(which doesn't have 'city' field) with my new DAO Object with an attribute 'city', i was not able to fetch the data.
Thanks
The idea is to use the version number that is passed to the onUpgrade(...) method. With ORMLite, the OrmLiteSqliteOpenHelper.onUpgrade(...) method takes an oldVersion and newVersion number. You then can write conversion code into your application that is able to convert the data from the old format and update the schema.
For more details visits docs http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_4.html#Upgrading-Schema
Most likely, you should make your schema changes conditional to the version you are upgrading from:
if (oldVersion < 2) {
// we added the age column in version 2
dao.executeRaw("ALTER TABLE `account` ADD COLUMN age INTEGER;");
}
if (oldVersion < 3) {
// we added the weight column in version 3
dao.executeRaw("ALTER TABLE `account` ADD COLUMN weight INTEGER;");
}
I am trying to create a static list of timestamps so that i can join them agains another table to create chart data. So far I have a query in this format SELECT * FROM (VALUES('a'),('b'),('c'),('d')) AS tbl ,which is working in sqlitestudio but not in android 4.4. When I run the query in the phone I get the error
android.database.sqlite.SQLiteException: near "VALUES": syntax error (code 1): , while compiling: SELECT * ...
I have also tried wrapping the values term inside another select like this SELECT * FROM (SELECT * FROM (VALUES('a'),('b'),('c'),('d'))) AS tbl but I still get the same error.
The full query now looks like this
SELECT * FROM (select * from ( VALUES (1458111312025),
(1455667200000),
(1455753600000),
(1455840000000),
(1455926400000),
(1456012800000),
(1456099200000),
(1456185600000),
(1456272000000),
(1456358400000),
(1456444800000),
(1456531200000),
(1456617600000),
(1456704000000),
(1456790400000),
(1456876800000),
(1456963200000),
(1457049600000),
(1457136000000),
(1457222400000),
(1457308800000),
(1457395200000),
(1457481600000),
(1457568000000),
(1457654400000),
(1457740800000),
(1457827200000),
(1457913600000),
(1458000000000),
(1458086400000))) i LEFT JOIN (
SELECT (osysdate- (osysdate % 86400000) ) interval, SUM(field004) totalval FROM onlineactivities WHERE field003 !=300000 and osysdate> 1455605711999 GROUP BY interval )
onl ON i.'' = onl.interval;;
Note that this works in sqlitestudio with sqlite version 3.8.10 but not in android kitkat (not sure about the sqlite version in it) What could be the problem?
Also please check out this question which is what I am trying to do but with sqlite and this answer
The VALUES syntax is available since SQLite 3.8.3, which is not available in every Android version.
To be compatible with earlier SQLite versions, you have to use a compound query:
SELECT 1458111312025 UNION ALL
SELECT 1455667200000 UNION ALL
...
Alternatively, put all the values into a temporary table.
I have created one Member_Master table whose primary key is member_id in sqlite manager for android and creating foreign key in child table e.g Event_Master for member_id from Member_Master. I was getting no such table found:Member_Master error
But now I am getting the following error :
SQLiteManager: CREATE TABLE "main"."Events_Master" ("event_id" NUMERIC PRIMARY KEY NOT
NULL ,"event_name" TEXT NOT NULL ,"event_details" TEXT NOT NULL ,"event_start_date"
DATETIME NOT NULL ,"event_end_date" DATETIME NOT NULL ,"event_time" DATETIME NOT NULL
,"event_venue" TEXT NOT NULL ,"min_level" NUMERIC,"status_id" NUMERIC,"member_id"
NUMERIC, foriegn key(member_id) references Member_Master(member_id)ON DELETE CASCADE ON
UPDATE CASCADE) [ near "member_id": syntax error ]
Exception Name: NS_ERROR_FAILURE
Exception Message: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE)
[mozIStorageConnection.createStatement]
Please give me the solution. Since Member_Master table is already created and field name is member_id only.
If you are getting no such table exception, the reason for this is that the table is not created.There can be few reasons like
1 ) The command you have used to create the table is is wrong (and could be a typing error also).
2 ) You are adding a table in an already existing database
When Database is created with specific number of tables you can not add more tables to it .
If you want to add more tables, you need to uninstall the application from your emulator and install the application again only then it shall create the new tables by re creating the database.
Or upgrade the database version , it shall drop all the tables once and then recreate the database.
Try uninstalling the app and reinstall it again. it works most of the times.
If you are getting no such table exception, then It is sure that the tables are not getting created.Have You written create Table query in your DatabaseAdapter class or simply adding a table from SqliteManager.
If you are creating tables dynamically, please write CREATE TABLE query and execute Query.
I have a database now i have to check whether it is empty or not. if empty i have to write the code for insert values otherwise i have to update the values of the fields.
please help me..
You can try something like this:
// Open or crare
myDB = this.openOrCreateDatabase("MyDatabaseName", MODE_PRIVATE, null);
//Check if exists (if not create the table)
myDB.execSQL("CREATE TABLE IF NOT EXISTS MyTableName (Name1 VARCHAR, Name2 VARCHAR);");
//Insert or update your lines
myDB.execSQL("INSERT INTO MyTableName (Name1 , Name2 ) VALUES ('XXX', 'YYY');");
Have you done the Google Notepad tutorials 1,2,3? they have a bunch of database help
Hope that can help
It's possible to include a .db file in your application package and use that as the starting point for your device-side database. It goes in the assets folder. You'll have to manually copy the values out of that .db into the one on the device in your startup method, but it saves putting a bunch of SQL INSERT calls in code.
The MOTODEV Studio Database perspective will assist you with this. You can use it to create a .db from scratch and populate it with your default values. Then, there is a wizard that will set up a content provider and accessor classes for the fields if you need them.
Disclaimer: I'm the product manager for MOTODEV Studio.