In order to insert data into a sqlite table named mytable in android I use query:
db.execSQL("INSERT INTO MYTABLE VALUES('','"+name+"',NOW());");
I want to check whether this query was successfully inserted into the table or not.
In php (with mysql) you could easily do
if($result)
echo 'success';
else
echo 'not inserted';
But what is the equivalent code in android (with sqlite) to check whether the query has been successfully
executed without any error?
According to the documentation execSQL throws an SQLException so I think your best bet is to surround your execSQL call with a try catch. You can then deal with whatever errors occurs in the catch. So something like this is what you want
try {
db.execSQL("INSERT INTO MYTABLE VALUES('','"+name+"',NOW());");
} catch (SQLException e) {
...deal with error
}
Related
I want to search on a scholar subject DB, and when I get the name of the subject, create a new table to save some homework there ( as a list to do) but this will be multiple (a scholar schedule).
I have been trying with on upgrade, I get the name, I create the table, but when I try to add smt it says "no such table" how I can do this?
THANKS A LOT
String stm = "CREATE TABLE something (colum1,colum2, ...)";
public void execSQL(String stm) {
try {
myDataBase.execSQL(stm);
} catch(Exception e) {
e.printStackTrace();
}
}
After insert your data in your colums. You can always use the same function, just create new statement variable each time you calling it. A simple easy way, not always the best, but it's work well. I suppose you know how to read data in your db. I hope it will help you.
I am working on Android Application and i am using Suger ORM for my database operation. Now i have scenario where i have to check if "SomeTable" don't exist then create it and Insert the record and if table already exist and having some record i have to update the records.
I have write this code to check if table don't exist then create the record and save it.
Total_Budget_List = Total_Budget.listAll(Total_Budget.class);
if (Total_Budget_List.size() == 0)
{
for (int i=0;i<Total_Budget_List.size();i++)
{
totalbudget = new Total_Budget(Select_Members.get(i).getId()+"",CurrentDate,per_person_budget+"");
totalbudget.save();
}
}
But i am getting no such table exist in the database.
Now how can i check that if table exist and is there any record in that table.
Surround your code with try / catch block. You can handle SQLiteException if your table not exists.
I really have two questions:
1/ how to read/check sqlite db table offline?
2/ Why is a sqlite table column of boolean type set to NULL?
ad 1/ I have downloaded sqlite3.exe and when i use the cmd > .dump quotes_table i see all rows like
INSERT INTO quotes_table` VALUES................
INSERT INTO quotes_table` VALUES................
INSERT INTO quotes_table` VALUES................
INSERT INTO quotes_table` VALUES................
etc
Is this correct as i was expecting to see just the values and not the queries?
ad 2/ I alter+updated the quotes_table by
db.execSQL("ALTER TABLE quotes_table ADD COLUMN quoteUsed boolean");
db.execSQL("UPDATE 'quotes_table' SET quoteUsed = 0");
I read on http://www.sqlite.org/datatype3.html sqlite use 0 and 1 for boolean type so i thought putting quoteUsed = 0 should be OK, but the value reads NULL.
How can i fix this?
regards,
ps: FYI just installed SQLite manager addon for Firefox and it makes things easier
1/ how to read/check sqlite db table offline?
Normally, SQLite is used with local databases, so database exists or does not exists - "offline" doesn't apply.
2/ Why is a sqlite table column of boolean type set to NULL?
SQLite doesn't have a boolean data type. It's affinity is NUMERIC. Check Datatypes In SQLite Version 3
NULL values, as in other DBMS, means nothing assigned.
when i use the cmd > .dump quotes_table i see all rows like ...
DUMP is used to extract all data from database. Actually, it returns SQL statements you may use to rebuild a new database from scratch.
To get data only, use SQL queries (check SELECT statement).
when i use the cmd > .dump quotes_table i see all rows like
...
Is this correct as i was expecting to see just the values and not the queries?
Yes, .dump creates the SQL that would produce an identical database.
2/ I alter+updated the quotes_table by
...
I read on http://www.sqlite.org/datatype3.html sqlite use 0 and 1 for boolean type so i thought putting quoteUsed = 0 should be OK, but the value reads NULL. How can i fix this?
Nothing really wrong with the SQL. How are you reading the value?
I want a conditional statement that determines if the table exists. Unfortunately the Cursor data type returns an uncaught exception (and crashes the app) before I can do a null comparison or check for false etc
What is the best way to determine from Java if values in my sql table exist
"What is the best way to determine from Java if values in my sql table exist?"
SELECT exists
(select * from T where mycolumn = {some value} )
will return 1 if true or 0 if false, in SQLite.
Try running this query:
SELECT * FROM dbname.sqlite_master WHERE type='table';
This queries the sqlite master table and you should be able to see the table you want there.
I have the following code to open a database on the SDCARD and then update a value. If I run the code as is, it will generally run through without throwing an error but nothing gets updated.
If I single step through each line, it works perfectly. I have also tried adding SystemClock.Sleep(2000) in between each statement. Still, it does not run properly unless I single step through this section.
Any ideas?
SQLiteDatabase db = openOrCreateDatabase( sdDIR + "/DBNAME.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.execSQL("UPDATE tableName SET value='" + newValue + "' WHERE name='field_name';");
db.close();
From the Android SQLiteDatabase documentation:
public void execSQL (String sql, Object[] bindArgs)
Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE.
You need to use update() or rawQuery().