I have a sqlitedatabase which has table lets say "ABC" and this table has a column lets say "X". While creating the table i mentioned the data type of column X to "text". And i store current milliseconds time in this column. But the problem is mentioned below.
I get the cursor by running the query on this table "ABC"
I check the cursor that it has got rows.
When i try to get the value of column "X" by using cursor.getString method then it throws the numberFormatException.
please suggest any reason for this...
Thanks
Dalvinder Singh
Related
I have a "logic" problem when making "inserts" on the SQLite database of my app.
These are my tables.
How should I manage the "need" of retrieving contact_id and photos_id in order to make the insert into the table contact_photo?
I know that I can get the last _id generated by SQLite with SELECT seq from sqlite_sequence where ..., but this doesn't seem a "professional" solution.
Is there a better way to achieve my goal?
Thank you in advance
have a look to this document
long insertOrThrow (String table,
String nullColumnHack,
ContentValues values)
will returns row ID of the newly inserted row, or -1 if an error occurred
I want to add a column to a table in case the column doesn't exist,but SQLite doesn't seem to update the column list in Android
currently i'm using:
DB.execSQL("alter table "+table_name+" add column "+column_name+" text");
DB.rawQuery("select * from "+table_name+" limit 1",null).getColumnNames();
but the string list returned from "getColumnNames" does not contain the column I just created, so when I check again for the same column, it understands the column doesn't exist and tries to create it again, which causes a "duplicated column" exception
Thanks in advance for any help,this is my first question in SO :)
You need to increase the database number, which will call onUpgrade on your next app launch. In your onUpgrade method, you run the SQLite command to alter the table.
See here for some examples.
Try to discard the result of the first query after the "alter table" command and use the result of a second query. It worked for me.
I'm working on a project, in which I'm creating a SQLite database table and it has certain number of columns. I want to include a condition that if a particular column is not null, then perform certain action (in order to fill that value up). I searched a lot, but didn't find anything. Please help.
Thanks in advance.
There is PRAGMA table_info(table_name) statement which will return for each column of table_name a result row with following informations (in separate columns):
Column name
Column type
If column can be null or not (that is if it's 0, then the column has NOT NULL constraint)
Default value for the column (will be null, unless column has DEFAULT constraint defined)
The third column would be answer to your question.
This is the query that I use to create a table
create table site_table(
_id integer primary key autoincrement,
name_site text,
url text,
login text,
pass text
);
I called Cursor.getColumnNames() and noticed that columns order are id, login, pass, name, url.
So, if I want a value I have to get it by the index Cursor.getString(index). Until I debugged I was messing up calling the wrong index, but now I wonder, why SQLite saves that way? Why it does not follow that way I created id, name_site, url, login and pass?
Thanks
So, if I want a value I have to get it by the index
Cursor.getString(index)
So for example for this reason you should always use
c.getString(c.getColumnIndex("ColName")); // or better getColumnIndex(CONSTANT)
This method saves all of us and ensure that you never get wrong results. Generally this method is recommended and also storing COLUMN_NAMES as CONSTANTS in separated class is very, very useful and efficient practise.
Note: Order depends on projection i.e. select name, lastname from table
That data is ordered by the order your requested it in your query, not the order you created the table with. So you probably changed the order in your query that generated said cursor.
Columns order in your cursor depends on projection. To be sure you use correct column index use c.getString(c.getColumnIndexOrThrow("COLUMN_NAME")) where c is your cursor.
I just made the experience first hand:
The indices of the columns of the cursor as a result of a
SELECT * FROM mytable WHERE ...
query have sometimes (not always) a different order that what SQLITE Database Browser shows as column order in the Database Structure tab. So referencing the columns via getColumnIndex seems to be the only safe way.
I have a databse with three tables.
The first table is something like:
_id user
The second table:
_id route user_id
and user_id is exactly the _id from the first table.
So when I insert in the first tabel a new record,I should keep the _id in order to insert it
in the second table.But how could I keep something that is autoincremented and given by the database?:-S
The SQLiteDatabase has an insert() method, which returns the _id you are looking for.
There may be a better way, but you could grab (query for) the _id field right after you do your commit to the first table. Then you will have the user_id for use in the second table.