while I am trying to execute my app, it is crashing and I am getting this "java.lang.RuntimeException: Unable to start activity ComponentInfo: Sqlite exception" error.
please help me find my mistake. here is the snapshot of my code:
#Override
public void onCreate(SQLiteDatabase db) {
this.db = db;
final String SQL_CREATE_QUESTIONS_TABLE =
"CREATE TABLE " +
questiontable.TABLE_NAME + " ( " +
questiontable._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
questiontable.COLUMN_QUESTION + "TEXT," +
questiontable.COLUMN_OPTION1 + " TEXT," +
questiontable.COLUMN_OPTION2 + " TEXT," +
questiontable.COLUMN_OPTION3 + " TEXT," +
questiontable.COLUMN_OPTION4 + " TEXT," +
questiontable.COLUMN_ANSWER_NR + "INTEGER" +
")";
db.execSQL(SQL_CREATE_QUESTIONS_TABLE);
fillquestionstable();
}'''
here is the logcat:
Process: com.example.modaltest, PID: 6534
java.lang.RuntimeException: Unable to startactivityComponentInfo{com.example.modaltest/com.example.modaltest.quiz}:android.database.sqlite.SQLiteException: near ".": syntax error (code 1): , while compiling: CREATE TABLE quiz_questions ( _id INTEGER PRIMARY KEY AUTOINCREMENT,com.example.modaltest.questionTEXT,option1 TEXT,option2 TEXT,option3 TEXT,option4 TEXT,answer_nrINTEGER)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
You have to change this line
questiontable.COLUMN_QUESTION + "TEXT," + to questiontable.COLUMN_QUESTION + " TEXT," +
space before TEXT
You have mistake in query check this
final String SQL_CREATE_QUESTIONS_TABLE =
"CREATE TABLE " +
questiontable.TABLE_NAME + " ( " +
questiontable._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
questiontable.COLUMN_QUESTION + " TEXT," +
questiontable.COLUMN_OPTION1 + " TEXT," +
questiontable.COLUMN_OPTION2 + " TEXT," +
questiontable.COLUMN_OPTION3 + " TEXT," +
questiontable.COLUMN_OPTION4 + " TEXT," +
questiontable.COLUMN_ANSWER_NR + "INTEGER" +
")";
questiontable.COLUMN_QUESTION + " TEXT," +
The problem is that in your line there is no distance between questiontable.COLUMN_QUESTION and TEXT as a result of which they merge into one line, which led to an error
There are two mistakes in your query.
1. First line questiontable.COLUMN_QUESTION + "TEXT," +
will be (give space before TEXT)
questiontable.COLUMN_QUESTION + " TEXT," +
2. Last line questiontable.COLUMN_ANSWER_NR + "INTEGER" +
will be (give space before INTEGER)
questiontable.COLUMN_ANSWER_NR + " INTEGER" +
Thank you.
There are 3 errors in the query.
The first is that you are using dots in the column name supplied in questiontable.COLUMN_QUESTION. In your example seems your are using "com.example.modaltest.question" as a column name, which can't work.
Only Alphanumeric characters and underline are allowed for columns names.
The column name must always begin with an alpha character or underline.
So, you need to replace "com.example.modaltest.question", by a proper column name which doesn't have any dot.
The other two errors are related to spaces. In the next declarations you need to include a space between the column name and the column type, as you have done with the other column declarations:
questiontable.COLUMN_QUESTION + "TEXT,"
and
questiontable.COLUMN_ANSWER_NR + "INTEGER"
Should be " TEXT", and " INTEGER", notice the space at the beginning of each string."
Related
This question already has answers here:
No such column found error while updating sqlite database
(1 answer)
android.database.sqlite.SQLiteException: no such column: name (code 1 SQLITE_ERROR)
(2 answers)
Closed 3 months ago.
I'm trying to make a query to my database involving the current day aswell as the current logged in user as such:
db.rawQuery("SELECT * FROM " + TABLE_NAME +" WHERE date = " + date + " AND user = "+user, null);
when i am logged in to the app with a user thats has a name only consisting of integers, such as 123 there is no problems getting the query, however when i log in using a user that has both integer and string values, or purely string values, such as test, i am met with an error message:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.afinal/com.example.afinal.ViewCourses}: android.database.sqlite.SQLiteException: no such column: test (code 1 SQLITE_ERROR): , while compiling: SELECT * FROM tasks WHERE date = 20221030 AND user = test
I have tried messing around with how the table is initialized, and i have set on this:
String query = "CREATE TABLE " + TABLE_NAME + " ("
+ ID_COL + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ USERNAME_COL + " TEXT,"
+ NAME_COL + " TEXT,"
+ LOCATION_COL + " TEXT,"
+ DATE_COL + " TEXT,"
+ TIME_COL + " TEXT)";
the variables USERNAME_COL and DATE_COL are "user" and "date" respectively
I have created the following table:
"CREATE TABLE ParsonCollection "
+ "(id integer not null GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),"
+ "name varchar(20),"
+ "eye varchar(20),"
+ "hair varchar(20),"
+ "height varchar(20),"
+ "weight varchar(20),"
+ "PRIMARY KEY (id))";
I am then trying to insert into the table and this is where I am running into issues. When I try to alter the "id" column, I get an error saying "java.sql.SQLSyntaxErrorException: Attempt to modify an identity column 'ID'. " Here is what the insert statement looks like:
"insert into ParsonCollection values(" + q_surround(Name) + ","
+ q_surround(Eye) + "," + q_surround(Hair) + "," + q_surround(Height) + "," + q_surround(Weight) + ",1" + ")";
However, when I take away the field that is inserting into "id", I get the following error: "java.sql.SQLSyntaxErrorException: The number of values assigned is not the same as the number of specified or implied columns." Here is what this insert statement looks like:
"insert into ParsonCollection values(" + q_surround(Name) + ","
+ q_surround(Eye) + "," + q_surround(Hair) + "," + q_surround(Height) + "," + q_surround(Weight) + ")";
How do I get past this? It seems that when I solve one exception, the other one pops up and vice versa. Thanks.
You can't assign to an identity column. Since you cannot pass all values for insert, you need to enumerate the columns (omitting the identity column):
"insert into ParsonCollection (
name,
eye,
hair,
height
weight
) values("
+ q_surround(Name)
+ "," + q_surround(Eye)
+ "," + q_surround(Hair)
+ "," + q_surround(Height)
+ "," + q_surround(Weight)
+ ")";
Side note: your code is opened to SQL injection. You should seriously consider using prepared statements and bind parameters instead of concatenating the query string.
Out of interest, another solution would be to make the identity column IMPLICITLY HIDDEN
String CREATE_SUBCATEGORY_TABLE = "CREATE TABLE " + TABLE_SUBCATEGORY_LIST+ "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," + KEY_NAME + " TEXT,"
+ KEY_DESC + " TEXT,"+ KEY_CATEGORY_ID + " INTEGER,"
+ KEY_CONTENT1+" TEXT,"+ KEY_CONTENTTYPE1 + " TEXT,"
+ KEY_CONTENT2+" TEXT,"+ KEY_CONTENTTYPE2 + " TEXT,"
+ KEY_CONTENT3+" TEXT,"+ KEY_CONTENTTYPE3 + " TEXT,"
+ KEY_CONTENT4+" TEXT,"+ KEY_CONTENTTYPE4 + " TEXT,"
+ KEY_CONTENT5+" TEXT,"+ KEY_CONTENTTYPE5 + " TEXT,"
+ KEY_CONTENT6+" TEXT,"+ KEY_CONTENTTYPE6 + " TEXT,"
+ KEY_ORDERID+" INTEGER,"+ KEY_STATUS+" TEXT,"
+ KEY_UPDATED+" TEXT, FOREIGN KEY ("+KEY_CATEGORY_ID+") REFERENCES "+CAT_TABLE+"("+KEY_CATEGORY_ID+")";
I am getting the following error now:-
android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: CREATE TABLE subcategory_list(id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,subcategory_name TEXT,subcategory_shdesc TEXT,category_id INTEGER,content1 TEXT,content_type1 TEXT,content2 TEXT,content_type2 TEXT,content3 TEXT,content_type3 TEXT,content4 TEXT,content_type4 TEXT,content5 TEXT,content_type5 TEXT,content6 TEXT,content_type6 TEXT,order_id INTEGER,status TEXT,updated TEXT, FOREIGN KEY (category_id) REFERENCES category_list(category_id)
Can anyone point out my mistake. Any help or suggestions are appreciated. Thank you.
Just fix some typos in the last line:
+ KEY_UPDATED + " TEXT," //added , and space
+" FOREIGN KEY (" + KEY_CATEGORY_ID + ") REFERENCES "
+ CAT_TABLE + "("+KEY_CATEGORY_ID+"))"; //added "CREATE TABLE closing parenthesis"
Add a Space before and a comma after "TEXT" in the last line.
You also have to close the last bracket.
Your last line would look correctly like this:
+ KEY_UPDATED+" TEXT," +" FOREIGN KEY ("+KEY_CATEGORY_ID+") REFERENCES "+CAT_TABLE+"("+KEY_CATEGORY_ID+"))";
You can also omit the " + " between TEXT and FOREIGN KEY, because you're just joining two strings.
+ KEY_UPDATED+" TEXT, FOREIGN KEY ("+KEY_CATEGORY_ID+") REFERENCES "+CAT_TABLE+"("+KEY_CATEGORY_ID+"))";
I want to connect to tables in my database with different ID's (CustomerID, OrderID). I use the following code to create my foreign key:
FOREIGN KEY("+ COLUMN_ORDER_ID + ") REFERENCES+TABLE_NAME_CUSTOMER + "(" + COLUMN_CUSTOMER_ID + "));";
I'm not posting the complete source code, because my first table works fine and I think the problem is the foreign key.
I filter my data with this method:
public List<Orders> getOrdersByCustomerID() {
List<Orders> orderList = new ArrayList<Orders>();
String query = "select " + COLUMN_ORDER_ID
+ "," + COLUMN_ORDER_NAME
+ "," + COLUMN_SETS
+ "," + COLUMN_REPEATS
+ "," + COLUMN_SECTION
+ " from " + TABLE_NAME_ORDERS
+ " where " + COLUMN_CUSTOMER_ID
+ "=" + COLUMN_ORDER_ID;
db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
Orders orders = new Orders(cursor.getString(0), cursor.getString(1), cursor.getInt(2), cursor.getInt(3), cursor.getString(4));
orderList.add(orders);
} while (cursor.moveToNext());
}
db.close();
return orderList;
}
I get this error message:
06-26 17:11:26.154 10163-10163/com.xxx.xxx.xxx
E/AndroidRuntime: FATAL EXCEPTION: main process: com.xxx.xxx.xxx, PID: 10163
java.lang.RuntimeException: Unable to start activity
android.database.sqlite.SQLiteException: no such column:
customerId(Sqlite code 1): , while compiling: select
orderId,orderName,sets,repeats,section from orders where
customerId=orderId,(OS error - 2:No such file or directory)
I think the connection between this two ids is incorrect, or must I commit the id from my customer table? Any tips? As per my understanding, a Customer can have multiple Orders. That's why I use the foreign key; I hope this is correct.
Edit: I was wrong apparently queries are case-insensitive by default in mysql.
Edit:
In order to create a foreign key both tables must contain the ConsumerId. This allows you the establish a relationship using the foreign key.
Your query has to be changed as wel, after creating the foreign key. It should be something like this
select " + COLUMN_ORDER_IDq
+ "," + COLUMN_ORDER_NAME
+ "," + COLUMN_SETS
+ "," + COLUMN_REPEATS
+ "," + COLUMN_SECTION
+ " from " + TABLE_NAME_ORDERS + "," + TABLE_NAME_CUSTOMERS
+ " where " + TABLE_NAME_ORDERS +"." + COLUMN_CUSTOMER_ID
+ "=" + TABLE_NAME_CUSTOMERS + "." + COLUMN_CUSTOMER_ID
I have 3 tables:
Table.Keys, Table.Tags, Table.Values
Table.Keys create table statement:
createTableStatement = "CREATE TABLE " + Tables.KEYS + "("
+ KeysColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KeysColumns.KEY + " TEXT NOT NULL,"
+ "UNIQUE ("
+ KeysColumns.KEY
+ ") ON CONFLICT IGNORE"
+ ");";
execSQL(sqLiteDatabase, createTableStatement);
Table.Tags create table statement:
createTableStatement = "CREATE TABLE " + Tables.TAGS + " ("
+ TagsColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ TagsColumns.NAME + " TEXT NOT NULL,"
+ "UNIQUE ("
+ TagsColumns.NAME
+ ") ON CONFLICT IGNORE"
+ ");";
execSQL(sqLiteDatabase, createTableStatement);
Table.Value create table statement:
createTableStatement = "CREATE TABLE " + Tables.VALUES + " ("
+ ValuesColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ ValuesColumns.KEY_ID + " TEXT NOT NULL,"
+ ValuesColumns.TAG_ID + " TEXT NOT NULL,"
+ ValuesColumns.VALUE + " TEXT NOT NULL,"
+ "UNIQUE ("
+ ValuesColumns.KEY_ID + ", " + ValuesColumns.TAG_ID
+ ") ON CONFLICT REPLACE"
+ ");";
execSQL(sqLiteDatabase, createTableStatement);
If I do the following join:
Tables.KEYS
+ " JOIN " + Tables.VALUES
+ " ON " + Values.KEY_ID + " = " + Keys.column(Keys._ID)
+ " JOIN " + Tables.TAGS
+ " ON " + Values.TAG_ID + " = " + Tags.column(Tags._ID);
I get duplicate rows of course because the result is
KEY | TAG | VALUE
=================
| |
What I would like to accomplish is to query and get a Cursor from table or view with no duplicate rows with the following schema:
KEY | TAG 1 | TAG 2 | ... | TAG n
=================================
| | | |
Not all keys MUST have values for each tag, but all keys CAN have values.
I'm not sure how to accomplish this. I'm not even sure where to start.
In the meantime I have created another table which stores some TAG values that I know will always exist.
But I feel this is inefficient because at any time I could have 'n' number of new TAG values which is why I would like to be able to create a view or table with the schema I listed.
SQLite has no pivot functions; you have to do this in two steps.
First, get all possible tags:
SELECT _id, Name
FROM Tags
ORDER BY Name;
Then, using the returned data, construct a query that looks up each possible tag for each key:
SELECT Key,
(SELECT Value
FROM Values
WHERE Key_ID = Keys._id
AND Tag_ID = 111
) AS Tag_111,
(SELECT Value
FROM Values
WHERE Key_ID = Keys._id
AND Tag_ID = 222
) AS Tag_222,
...
FROM Keys;