I am following this tutorial: http://www.codeproject.com/KB/android/AndroidSQLite.aspx
I must be overthinking this SQLite stuff (in the past my domain server would automatically initialize databases I requested, and I could do queries when desired. never put one together from scratch)
I have some questions about their onCreate function. I never recall using a
CREATE TRIGGER command in my SQL
I only need to create one table with 2 or 3 columns (if you count the primary key)
I should just be able to do
db.execSQL("CREATE TABLE" + tableName +"("+colID+"INTEGER PRIMARY KEY,"+columnName+"TEXT)");
correct?
Do I need a "Trigger" and a "View" ?
If you just need a place to store some data - then Table is enough. But if your logic is more complicated then you'll need additional stuff. Also note that some Triggers are not supported by SQLite: Info from here
You not need to create TRIGGER. Unless it is required. Here is how I implemented in one of my project. Hope this help.
https://github.com/gopalB/FeedReader/blob/master/src/com/feedReader/provider/FeedDB.java
If you do not need a Trigger or a View, then you do not need to create them. It appears that the tutorial is just explaining some of the things you can do.
if SQLite TRIGGER and VIEW are similar to what they're used for in MySQL then no, they are not necessarily for what you're trying to accomplish.
VIEWs are useful when you have complex queries (like when using JOINs to join data from multiple tables).
TRIGGERSs are conditions that are run when you modify a table. (like using UPDATE, or INSERT)
As written, your create statement won't work because of a lack of whitespace. Try:
db.execSQL("CREATE TABLE " + tableName +" (" + colID + " INTEGER PRIMARY KEY, " + columnName + " TEXT)");
Related
I have an Sqlite database on Android and if I put 2000 records in one table all the other tables get really slow.
With 2000 records in the one table I can run a 'SELECT COUNT(*)' on a table with 0 records and it can take anywhere from 5 to 30 seconds.
There can be many reasons for that. The usual reasons are:
Poorly written query
Not using a primary key, assuming one even exists on the table
Poorly designed data model (table structure)
Lack of indexes
IMO, your case may be lack of indexes. Check if you have indexed your tables/rows.
"SELECT COUNT( * )", the char ' * ' need database to extract every field when sql execute.
As an additional suggest, you can use primary key in the " count( < your primary key > ) ".
i am getting information from user in sqlite database.
But when i insert same record which is already in database it is added again.
how i can stop duplication of record in sqlite. I am developing this in android.
I am using mobile number as primary key. still it add that record in database.
Please suggest me appropriate solutions.
Thanks in advanced.
Be aware of the limitations of REPLACE or INSERT OR REPLACE as these will overwrite any custom data your app user has added to these rows in the database - it is not as advanced as UPSERT in other SQL databases.
As mentioned in a previous post you really need to identify what the primary key could be and use this information to either update old data or to remove an old row before inserting the fresh one.
If this is not possible then you could always DELETE FROM my_table or DROP my_table before running the insertions so that there will be no duplicates. This will (for better or worse) also make sure that data that is missing from new imports is not left lying around in your app.
make sure you have set your phone number as Primary Key at the time you created the table.
for example:
String query = "CREATE TABLE IF NOT EXISTS PhoneBook ("+
"TelNum VARCHAR(100) PRIMARY KEY NOT NULL,"+
"Address TEXT);";
db.execSQL(query);
and in case you want to enforce foreign keys defined in your table then call the following method before doing anything in your database
db.execSQL("PRAGMA foreign_keys = ON;"); //enforcing FK
Use REPLACE INTO keyword:
REPLACE INTO my_table (pk_id, col1) VALUES (5, '123');
This automatically identifies the primary key and finds a matching row to update, inserting a new one if none is found.
I am missing something with all of the android SQLite tutorials.
I want to create a SQLite database that holds the autoincrementing key, and four text fields that I will pass in
I intend to pass in this SQL database
private static final String INSERT = "insert into " + TABLE_NAME + "(field1) values (?)" + "(field2) values (?)" + "(field3) values (?)";
but I'm not sure if the android sdk has a proper insert function.
I'm not sure how "Cursor" relates to anything I am trying to do, and I'm not sure how much object oriented initializing I should be trying to as opposed to just calling some built in android sdk functions.
insight appreciated, but please break it down
Will recommend you to go through NotePad exercise here is a link. Specially go through Exercise 1.
And for more depth knowledge you can go through project which I have created. Here is a link
Look for creatFeed function call here. I think will help to answer your problem.
I want to force a foreign key constarint on a table in an Android application.
I've searched that this can be done by using triggers:
I did it like this:
db.execSQL("CREATE TRIGGER dept_id_trigger22+" +
" AFTER INSERT "+
" OF EmployeeName ON Employees"+
" BEGIN"+
//Condition
" RAISE(ABORT,'error') END;");
but no error was raised and the illegal values are inserted.
what is wrong with this ?
Ok I got it
Android supports SQLite triggers.
The correct syntax is
db.execSQL("CREATE TRIGGER dept_id_trigger22" +
" AFTER INSERT "+
"ON Employees"+
" BEGIN"+
//Condition
" SELECT RAISE(ABORT,'error'); END;");
I forgot to add semicolon after the raise statement.
This does not execute the statement but it does not throw an exception.
still will search for how to throw exceptions
thanks
Foreign keys are only supported on Android on Froyo (2.2) or newer, for previous versions you can include them but SQLite ignores them. All Android versions of SQLite support triggers to produce the same effect though.
Newer versions of SQLite (for your PC) has a command called "genfkey" that will analyze your SQLite database (which has foreign keys in it) and produce the equivalent triggers. This way you can design your tables with foreign key constraints while also supporting all versions of the OS.
On Windows, open the SQLite command line tool with your database file as a parameter:
sqlite3 mydatabase.db
.genfkey --exec
This will generate triggers for all of your key constraints.
I don't expect any votes for this answer, just to let you know:
You could use another database, for example the H2 database. Disclaimer: I'm the main author of H2.
There are some disadvantages: some (not all) operations are slower, for example opening and closing a database. The jar file is relatively big (about 1 MB). You would have to use the JDBC API.
But the advantage is: H2 supports using triggers, constraints, and so on.
To delete Last 50 rows when count is greater than 100
sqliteDB.execSQL("CREATE TRIGGER IF NOT EXISTS delete_trigger
AFTER INSERT ON table1
WHEN (SELECT COUNT(*) FROM table1) > 50 " +
BEGIN
delete From table1 where id not in(select id from table1 order by id desc limit 100;
END;"
);
I discovered that the SQLite version used does not support foreign keys - so I expect that triggers are not supported, too.
I currently facing problem where user have the choice to enter several working experience, where every working experience include several details (company name, duration, roles etc)
I don't know how should I store it since users have the freedom to add as many working experience as they have.
I have a table as below
private static final String DATABASE_CREATE_WORKEXP =
"create table workexp (work_id integer primary key, _id integer not null," +
"workcompany text, workduration text, workrole text, workskills text" +
"workproject text, worksalary integer, workreason text," +
"foreign key(_id) references user(_id));";
I have a problem while trying to insert/update and delete entries as company name can be duplicated.
Any advice or suggestion?
I'm glad to provide more info for you to help me.
THANKS!
If you want to avoid duplicate company names, they should be managed in their own table. Then you'll have to add some UI to pick a company name in the table of existing companies or create a new one.
every db table should have an id field, which has the primary_key attribute. the id is unique and can not be dublicated. never use a name as primary key unless it is not unique.
Before implementing databases in android think about your idea.
First you can store the data on the device. But this solution has a big disatvantage. If your app is deleted from the device all data is lost.
On the other hand you can store the data online, on a webserver and access it through a REST API which I think is a more comfortable solution. It is up to you.