Example of how to implement ALTER TABLE [duplicate] - android

This question already has answers here:
adding a column to SQLite table in Android?
(3 answers)
Closed 8 years ago.
Ive asked this question several times and yet to get a COMPLETE answer... How to you implement the ALTER TABLE statement to add a column to a database. Can someone PLEASE give me an example?

Please read the SQLite ALTER TABLE reference, and this Android SQLite article:
database.execSQL("ALTER TABLE " + your_table_name + " ADD COLUMN " + new_col_name + " int");

ALTER TABLE db_name.mytable ADD COLUMN column_name VARCHAR(30)
That's for SQLite. Other databases may allow you to specify where the column gets put, but with SQLite it's appended to the end.
EDIT:I thought about this some more. In your question you asked for a complete answer. And someone identified that you asked essentially the same question just 5 hours before asking this one. Considering that none of the responses in that previous thread were what you were looking for (despite some of them offering complete SQL examples), it strikes me that a complete answer must include something more than the SQL and a few links to web tutorials.
In an effort to be thorough and complete, let me suggest that you consider the following books: Using SQLite (O'Reilly), and Learning Android (O'Reilly). In fact, the latter has a chapter (chapter 9) devoted to the Android database environment, meaning Android's implementation of SQLite.
People who have a lot of experience with databases, with programming, and with diverse operating systems often can get by with web manuals and a few tips on sites such as StackOverflow. But for the rest of us (myself included), when I really want to learn a topic that I consider important to me, I immerse myself in a couple of good books along the way.
I hope that you, or someone else who may be just getting started with such topics, find this advice helpful.

I've just encountered this and tried over and over and got the exception described here about column not exists, despite the alter table statement didn't throw any exception.
After two hours of trying, setting up a file backup storage, and whatever seemed viable here, I realized that I didn't include the new column in the "select query", but still tried to extract the data from the cursor, hence the column does not exist exception.

Related

Android Sqlite Changing Database?

I am a beginner android learner who has been given a very challenging but interesting project to do . I have to write a program to give different quizes but the difficult part is the changing database. Any teacher who wants to use it write his questions in a C# software then it generates a sqlite database as an output to be given to the android app and then the teacher will be able to give different exams from his students on their android phones . The point is students install the apk just once and from that time on, they just be given the sqlite file to be read by their android app . how is it possible to read sqlite in a way like mentioned?
Tnx in advance .
Your C# program needs to generate DDL, which you can then execute in your Android app. Your Android code might look something like this:
String ddl = "CREATE TABLE [Quiz] (" +
"[id] INTEGER NOT NULL ON CONFLICT ROLLBACK PRIMARY KEY, " +
"[question] CHAR NOT NULL)";
db.execSQL (ddl);
If you want to see samples of DDL, install Sqlite on on your development machine and install a sql tool, such as SQLite Expert, to create and view a database.
In general I think this is not a good design approach. It would be easier to read the quiz data from a HTTP-Request. But if you are not in a position to change the requirements you are searching for an approach to import a sql-backup at runtime like described here:
https://stackoverflow.com/a/6542214/1515052
If this does not fit your needs exactly, you should do your research first, start an implementation and rephrase your question where exactly lies your problem.

Is it better to avoid OR clauses in database queries? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Assume I have a database set up and a table named MyTable which contains a large number of records (tens of thousands). Assume a query as follows...
select * from MyTable where ColumnX = 'X'
... returns just a handful of records (< 10). Let's say I wanted to filter this result set further to only those records where ColumnY matches 'Y1' or 'Y2'. Is it better from a speed and memory perspective to simply modify to the above query as follows...
select * from MyTable where ColumnX = 'X' and (ColumnY = 'Y1' or ColumnY = 'Y2')
... Or is it better to iterate over the (small) result set in code and filter out only those records where ColumnY matches 'Y1' or 'Y2'? The reason I ask is because I have been told that OR clauses are bad in database queries from a performance perspective (when dealing with large tables) and better avoided where possible.
Note: The scenario in which this applies for me is an Android application with a local SQLite database but I guess the question is a bit more generic than that.
SQLite's documentation describes multiple optimizations that can be done on queries with OR, and says:
For any given query, the fact that the OR-clause optimization described here can be used does not guarantee that it will be used. SQLite uses a cost-based query planner that estimates the CPU and disk I/O costs of various competing query plans and chooses the plan that it thinks will be the fastest. If there are many OR terms in the WHERE clause or if some of the indices on individual OR-clause subterms are not very selective, then SQLite might decide that it is faster to use a different query algorithm, or even a full-table scan. Application developers can use the EXPLAIN QUERY PLAN prefix on a statement to get a high-level overview of the chosen query strategy.
In any case, implementing the OR by hand in your code is very likely to be slower than letting the database do it, because the database has to read and return all rows that match on ColumnX, even those that will not match on ColumnY.
Furthermore, the database already has code to do this filtering; implementing it again just increases the complexity of your code and the chances of errors.
The statement that "OR clauses are bad in database queries from a performance perspective (when dealing with large tables) and better avoided where possible" is not quite true; if you need the OR, all alternatives are worse.
You can try with IN clause :
select * from MyTable where ColumnX = 'X' and ColumnY in ('Y1','Y2')
Yes, Ajay you are right, thank you .
Another way to solve this will be to either use temp table, or With clause.
-- Following solution for Oracle , need little change for each db product to replace DUAL
with YT (columnY)( select 'Y1' as columnY from DUAL
union
select 'Y2' from DUAL)
select MT.*
from MyTable as MT
, YT
where MT.ColumnX = 'X'
and MT.ColumnY = YT.columnY

xamarin android best practises to tables with multiple columns

Maybe its a easy question but i cant figure out how to do tables with multiple columns and rows.. Order data in this table will be get via wcf service and next present in table. In this table i need of course scrolling and filtering. Every row will link in to details about order.
I thinking about using table layout and put in lists but this not resolved my problem with scrolling.. So should i do this? Which is best practise in this case topics?
I attach picture with this table.
Thanks for help.
I think i found solution of this problem on xamarin forum.
If samebody have better idea it will be pleasure to hear it.. :)
XamarinForum

SQLite DB design for Android App

I am building a database for an Android app using SQLite. The database consists of Zip code long/lat information. At runtime, a row will be added to a table in the database. I am trying to find the best way to design the database for both scalability and speed. This app will be released for all US states.
My question is this:
Would it be better to create a table for each US zip code, which stores this new information (and query the DB at runtime)
Or, would it be better to create 1 table in which all new data is added (and query the DB at runtime)?
-I might have answered my own question here, but the data added will only be kept in the database for approx. 1 week (most likely only a few days).
Any advice would be great!
One table that contains all zip codes is better. Otherwise you would have a maintenance nightmare on your hands and the code itself would be more complex. SQL queries are designed to be very fast as long as you have proper indexing.

Database query for comparision

Hi please anybody please help me with some link that will give details about comparing 2 columns in sqlite table.Actually my requirement is i have to compare 2 columns in 2 different table and i have to retrieve that particular row.Please help me.
If you're looking for resources on performing an INNER JOIN, look here: How do I join two SQLite tables in my Android application?
Please provide more information if this is not correct. Your question is hard to interpret.

Categories

Resources