I am trying to create one application to store information in sq-lite table.
But i want to take table information from user like table name, columns name, and number of columns, also user can edit that by table name, delete column add new column.
how can i do it.
i add example screen for table filed
Android allows you to execute arbitrary SQL strings. Construct a SQL strings from your UI and then input the string into a command similar to db.execute(string).
Reference
Related
How can I change the data type of one column to another?
I want to change the data type of one column form Number to Text of existing table on database upgrade.
As far as I know you cannot change it, so easily with an alter table or something like that because there are some restrictions in SQLite:
Complete ALTER TABLE support
Only the RENAME TABLE and ADD COLUMN variants of the ALTER TABLE command are supported. Other kinds of ALTER TABLE operations such as DROP COLUMN, ALTER COLUMN, ADD CONSTRAINT, and so forth are omitted.
Omitted information in SQLite
However, I have some advice based on my previous experience where I have needed to recreate tables:
To create a BackUp of the current information (storing the information in a XML or Cursor, it's just temporal). More information about cursors: Accessing Data With Android Cursors
To drop the table that you want to change.
db.execSQL("DROP TABLE IF EXISTS YourTableName");
To recreate again the table with the new structure.
db.execSQL("CREATE TABLE YourTableName");
To insert again the old information with a loop.
WATCH OUT: remember that you need to cast the column that before was a Number and now is a Text.
I'm trying to build a chore app where the user can create a group with a password and they can then add chores to the group that they made. My question is I'm using sqlite as the database and I'm wondering if it's possible to add a database column into the table. I'm planning on creating a table inside my db with columns of id, username, password, database. database is where I will create additional databases where it will store the chore table for my app. If I can't do this, can someone lead me along the way on how I can for example, make a table named Group, then for its columns have id, username, password. Then for each Group rows, be able to somehow match it with another corresponding database or table where the table will be chores with columns say name, frequency. And however many Group rows I add, I will need to somehow make another chore table to match with it, but I need to know how to link and refer to it from just my Group table. If there's a better way to do this using SQLite I would love to know. Thank you.
If i get your question correctly, I think all u need is to use PRIMARY_KEY and FOREIGN_KEY. Hope you are aware of constraints in databases.
So basically two tables will share a KEY which is unique, the key will be the PRIMARY_KEY in table 1 and will act as a link or reference to row in second table, where it is a FOREIGN_KEY
check this for more on constraints
I'm a bit unfamiliar with updating SQLite Databases after I have created them. Is it relatively easy to add new columns and update values in the rows? Like adding an email or twitter name column to a contact list app. Or could I set up columns with the name "Col1, Col2, etc" to have extra for later and then when I want to use those columns add them into my queries/cursors in my app such that KEY_EMAIL is "Col15" or whatever.
Sqlite supports a limited subset of the ALTER TABLE command. You can rename a table or add a new column. So you can't rename or delete a column or remove constraints after you've created the table (at least not with sql-commands).
So, no need to add "spare"-columns. In fact, since you can't remove columns, that'd be a bad idea.
SQL As Understood By SQLite
I am planning to build an app that lets the user select a record from a particular database such as:
name favorite_color favorite_team
sue red Dal
mike blue Mia
sam purple Bal
My problem is that most of the tutorials that I have come across only demonstrates examples using a table with one column. What if my pre-populated database more then on column? What if it had 100 columns? Does anyone know how this is done?????
Here's an example:
Step1:Declare SQLiteDatabase,Declare Databasehelper
Step2:Declare string or number which is to be used as key or get it as an intent(in this example 'rowid'
Step 3: In the OnCreate method add lines of code similar to following:
String query="select * from my_table_name where _id="+rowid;
Cursor myCursor = database.rawQuery(query,null);
myCursor.moveToFirst();
//This line implies i am getting data from column four of selected row
String x=myCursor.getString(4);
//This line implies i am getting data from column two of selected row
String y=myCursor.getString(4);
myCursor.close();
Note:
a)Don't forget that your database size must not exceed 1.2mb
b)Also include a column with name _id which auto-increments in each table that you are using,you may use sqlite browser to do so
c)also create the following table in your database :
CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US')
Now insert a single row with the text 'en_US' in the "android_metadata" table
INSERT INTO "android_metadata" VALUES ('en_US')
read this you will learn every thing you need to know. One other way to access db information is using an ORM like ormlite. I'm using it in various apps that i've developed, and it's simple to use.
The Android app that I am currently working on dynamically adds columns to an SQLite database. The problem I have is that I cannot figure out a way to remove these columns from the database.
If I add column A, B, C, D, and E to the database, is it possible to later remove column C?
I have done a lot of looking around and the closest thing I could find was a solution that requires building a backup table and moving all the columns (except the one to be deleted) into that backup table.
I can't figure out how I would do this, though. I add all the columns dynamically so their names are not defined as variables in my Java code. There doesn't seem to be a way to retrieve a column name by using Android's SQLiteDatabase.
SQLite has limited ALTER TABLE support that you can use to add a column to the end of a table or to change the name of a table.
If you want to make more complex changes in the structure of a table, you will have to recreate the table. You can save existing data to a temporary table, drop the old table, create the new table, then copy the data back in from the temporary table.
For example, suppose you have a table named "t1" with columns names "a", "b", and "c" and that you want to delete column "c" from this table. The following steps illustrate how this could be done:
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE t1_backup(a,b);
INSERT INTO t1_backup SELECT a,b FROM t1;
DROP TABLE t1;
CREATE TABLE t1(a,b);
INSERT INTO t1 SELECT a,b FROM t1_backup;
DROP TABLE t1_backup;
COMMIT;
SQLite doesn't support a way to drop a column in its SQL syntax, so its unlikely to show up in a wrapper API. SQLite doesn't often support all features that traditional databases support.
The solutions you've identified make sense and are ways to do it. Ugly, but valid ways to do it.
You can also 'deprecate' the columns and not use them by convention in newer versions of your app. That way older versions of your app that depend on column C won't break.
Oh... just noticed this comment:
The app is (basically) an attendance tracking spreadsheet. You can add
a new "event" and then indicate the people that attended or didn't.
The columns are the "events".
Based on that comment you should just create another table for your events and link to it from your other table(s). You should never have to add columns to support new domain objects like that. Each logical domain object should be represented by its own table. E.g. user, location, event...
Was writing this initially. Will keep it if you're interested:
Instead of dynamically adding and removing columns you should consider using an EAV data model for that part of your database that needs to be dynamic.
EAV data models store values as name/value pairs and the db structure never needs to change.
Based on your comment below about adding a column for each event, I'd strongly suggest creating a second table in which each row will represent an event, and then tracking attendance by storing the user row id and the id of the event row in the attendance table. Continually piling columns onto the attendance table is a definite anti-pattern.
With regards to how to find out about the table schema, you can query the sqlite_master table as described in this other SO question - Is there an SQLite equivalent to MySQL's DESCRIBE [table]?
As per SQLite FAQ, there is only limited support to the ALTER TABLE SQL command. So, the only way you can do is that ou can save existing data to a temporary table, drop the old table, create the new table, then copy the data back in from the temporary table.
Also you can get the column name from the database using a query. Any query say "SELECT * FROM " gives you a cursor object. You can use the method
String getColumnName(int columnIndex);
or
String[] getColumnNames();
to retrieve the names of the columns.