I have two tables in database, entries that only have column words, and another table worddictionary with columns word, definition and auduiourl,
String create_table_query = "CREATE TABLE `entries` (`word` varchar(50) NOT NULL)";
db.execSQL(create_table_query);
String create_word_table = "CREATE TABLE `worddictionary` (`word` TEXT NOT NULL,'definition' TEXT, 'audiourl' TEXT)";
db.execSQL(create_word_table);
I am trying to insert the words, its definition and audiourl that I got using web scraping into worddictionary table,
String insert_query = "INSERT INTO worddictionary values("+word+","+wordDefinition+","+audioUrl+")";
db.execSQL(insert_query);
But I get below error,
2020-07-13 14:11:56.527 27078-27131/com.example.scraping_synonyms E/SQLiteLog: (1) near "large": syntax error
2020-07-13 14:11:56.529 27078-27131/com.example.scraping_synonyms E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.scraping_synonyms, PID: 27078
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:354)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)
Caused by: android.database.sqlite.SQLiteException: near "large": syntax error (code 1 SQLITE_ERROR): , while compiling: INSERT INTO worddictionary values(aardvark,noun,
a large, nocturnal, burrowing mammal, Orycteropus afer, of central and southern Africa, feeding on ants and termites and having a long, extensile tongue, strong claws, and long ears.
,https://static.sfdict.com/audio/A00/A0001900.mp3)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1770)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1698)
at com.example.scraping_synonyms.MainActivity$Content.doInBackground(MainActivity.java:140)
at com.example.scraping_synonyms.MainActivity$Content.doInBackground(MainActivity.java:74)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
In this error, it is mentioned error near "large", that is the word in the definition I don't understand what the error is about
Could this be because the marks around word
are different than definition
?
And the guy I replaced used Content values so for you it would be
ContentValues values = new ContentValues();
values.put("word", word);
values.put("definition", definition);
values.put("audiourl", audiourl);
db.insert("worddictionary", null, values);
You want to insert a row of 3 columns in the table worddictionary
but what you do is trying to insert only 1 string by concatenating the strings word
, wordDefinition
and audioUrl
.
The correct syntax for an INSERT
statement is this:
INSERT INTO tablename(col1, col2, col3) VALUES (value1, value2, value3)
and if any of the values is a string it must be enclosed in single quotes .
So to be syntactically correct you should enumerate all the column names after the table's name and insert each column separately:
String insert_query = "INSERT INTO worddictionary (word, wordDefinition, audioUrl) values ('" +
word + "', '" + wordDefinition + "', '" + audioUrl + "')";
db.execSQL(insert_query);
But the recommended and safe way of doing this is with the method insert()
with ContentValues()
which takes care of quotes in these cases:
ContentValues cv = new ContentValues();
cv.put("word", word);
cv.put("definition", wordDefinition);
cv.put("audiourl", audioUrl);
int result = db.insert("worddictionary", cv);
You can check the variable result
after the insertion.
If it is -1
then an error occurred.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.