I am trying to insert text messages from an inbox to a SQLite Database, but due to some special characters/symbols it doesn't insert properly.
I went through some question in Stackoverflow but nothing seems useful.
I got this code, but it is not working.
data.execSQL("INSERT INTO recor(text) VALUES('"+DatabaseUtils.sqlEscapeString(messag)+"')");
My database has only one field in it and am trying to insert details of messages along with it. I am putting the details (type, time, number, message) to a single string message and recor is my table name.
This is what I get as toast when I use a try catch loop.
Error is near:
"FROM":syntax error:INSERT INTO recor(text) VALUES("FROM 15555215556 Message:-MSG")
Uses the DatabaseAdapter's insert method instead. e.g.
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, value);
dbAdapter.insert(TABLE_NAME, null, values);
it looks like your column name is 'text'? this must be wrong, as text is a keyword in sqlite.
Your final SQL string seems to include two sets of quotes around the string you are inserting, so I assume the DatabaseUtils.sqlEscapeString method adds its own quotes around the string.
Therefore, your code should be:
data.execSQL("INSERT INTO recor(text) VALUES("+DatabaseUtils.sqlEscapeString(messag)+")");
Related
Mistakenly I have put a double value divided by zero in an SQLite database column.
In "DB Browser", it looks like string "Inf" or "inf". However, "WHERE MY_COLUMN = 'inf'" gives no result.
How can I retrieve all records containing this value via an SQL query?
Also, how can I identify this value in the cursor loop?
I have tried these:
cursor.getString(cursor.getColumnIndex(MY_COLUMN)).equalsIgnoreCase("inf")
cursor.getDouble(cursor.getColumnIndex(MY_COLUMN)) == Double.POSITIVE_INFINITY
No result as well.
I just want to insert values into a table if the value provided does not exists in that table, I mean I have provided a column as UNIQUE, so sqlite3 UNIQUE constraint will be broken when that value is tried to input twice, i want an sqlite3 insert statement which helps to do this my code is. I read that INSERT IGNORE is used for this purpose. Can somebody provide me with syntax to do this correctly?
My code is given below.
String insertQuery1 = "INSERT INTO Bookdetails bookpath,lastchapter VALUES(?,?)";
db.execSQL(Query1, new String[] { filepath, none });
What is the correct syntax for this query? filepath and none are string which have values assigned
Also this table Bookdetails has a primarykey field 'id' which is auto increment? will it create any problems when data is inserted like this way?
String insertQuery1 = "INSERT INTO Bookdetails (bookpath,lastchapter) VALUES(?,?)";
db.execSQL(Query1, new String[] { filepath, none });
Change
INSERT INTO Bookdetails bookpath,lastchapter VALUES(?,?)
to
INSERT OR IGNORE INTO Bookdetails (bookpath,lastchapter) VALUES(?,?)
The OR IGNORE causes that when a constraint such as UNIQUE is violated, the insert doesn't take place and there won't be an error.
The column names to insert to need to be in () parens.
Additionally, you're passing some other query to execSQL() than the insertQuery1 here.
Also this table Bookdetails has a primarykey field 'id' which is auto increment? will it create any problems when data is inserted like this way?
No, since no insertion takes place.
If you had INSERT OR REPLACE instead of the ignore, it would translate to a DELETE followed by INSERT, generating a new row id in case the id was not specified in the insert itself.
The query is wrong:
INSERT INTO Bookdetails bookpath,lastchapter VALUES(?,?)
It should be:
INSERT INTO Bookdetails (bookpath, lastchapter) VALUES (?, ?)
Mind the parentheses surrounding the fields list!
I was curious if androids SQLiteDatabase insert method automatically handles type conversion.
Here is my example:
I have a csv file with a column name of age. Its type will be an INTEGER.
Lets say I have already created the database and table.
Now I am parsing the csv file with CSVReader, which parses each line and inserts each value into an index of a String[].
In order to insert each line of data into the database, I have to use a ContentValue object, which allows me to store values in it.
//Parse each line and store in line...
ContentValue values = new ContentValue();
values.put(KEY_AGE, line[1]); // Assume line[1] is the age
database.insert(table, null, values);
If I store the age value as a string (as seen above), and then insert it into the table, does Android handle the conversion to INTEGER before inserting it into the database?
I am asking this because I am trying to insert a bunch of tables into a database, and it looks much cleaner when I can just iterate through an array then explicitly state each put call, i.e:
Also if anyone has any design suggestions feel free to tell me.
CLEAN
int i = 0;
for(String s : TransitContract.Routes.COLUMN_ARRAY) {
values.put(s, line[i]);
i++;
}
UGLY
values.put(TransitContract.Routes.KEY_ROUTE_ID, line[0]);
values.put(TransitContract.Routes.KEY_AGENCY_ID, line[1]);
values.put(TransitContract.Routes.KEY_SHORT_NAME, line[2]);
values.put(TransitContract.Routes.KEY_LONG_NAME, line[3]);
values.put(TransitContract.Routes.KEY_DESCRIPTION, line[4]);
values.put(TransitContract.Routes.KEY_ROUTE_TYPE, Integer.parseInt(line[5]));
values.put(TransitContract.Routes.KEY_URL, line[6]);
values.put(TransitContract.Routes.KEY_COLOR, line[7]);
values.put(TransitContract.Routes.KEY_TEXT_COLOR, line[8]);
return mDatabase.insert(TransitContract.Routes.TABLE_NAME, null, values);
When you declare a column as INTEGER, SQLite will automatically convert strings to numbers, if possible.
See the documentation for Type Affinity.
If your ContentProvider doesn't restrict it (i.e. pass it directly to the SQLiteDatabase.insert() method), it should work. SQLite is not that picky about the types used in queries/inserts and the actual column type.
However, it would be best practice to parse and check the values before inserting. Otherwise you might actually insert a string which can't be parsed as integer and therefore retrieving the value might fail.
References:
Boolean datatype accepting string value and integer value
SQLite table with integer column stores string
I have a case that I would like to insert record in SQLite with database.insert(table, null, values).
TABLE t2 (_id, field1, field2)
..
val.setVal1(null);
val.setVal2(val2);
..
if(val.getVal1==null){
values.put(field1, _id);
}else{
values.put(field1, var.val1);
}
values.put(field2, var.val2);
database.insert("t2", null, values);
Is possible to do sth like this "values.put(field1, _id);"?
_id is generated at database.insert().
Note: I am looking for solution for one insert call. Insert and update row with (field1=_id) is easy.
i think i see now. you're asking if you can enter a value into a specific SQLite row _id field if it's available in your val object. Else, you want the database to automatically create a unique id for that column while inserting, like normally done. Is this correct?
To that end, i would seriously reconsider this purpose. You should never be specifying values for the _id column because it needs to be unique or else you'll get exceptions thrown. Moreover, it's only purpose is to be a unique identifier for the system, so you personally knowing this value should be of no use to you.
If you still need this functionality, i'd suggest making another field in your table (much like the _id column but not it), which you can fill with randomly generated numbers or val.getVal1 values.
Is it possible to take data from a edit text field and insert it into a SQLite db as a constant. I'm am trying to insert data into a SQLite primary key table and need for that data to be a constant for the db to accept it. I can't find any examples that would how me how to accomplish this.
i gotta say, i'm not quite sure what you mean by constant. just about every tutorial advises that you insert into the database with this method:
ContentValues values = new ContentValues();
values.put(DB_COLUMN, EtString);
long id = db.insert(DB_TABLE, null, values);
EtString is the string you get from the edittext field (with .getText, .toString or otherwise). This method would also provide a id for the string in the table that you could reference. But just know that if there's an _id field already in your database table, each row of information already gets an id without you doing anything.
Here's a good tutorial for this stuff:
http://www.vogella.com/articles/AndroidSQLite/article.html