I'm trying insert data from edittext into DB but i get these errors and can't solve it. I created DBHelper which code is below, just like class for adding data in DB. Also constructors and getters and setters. I really need some help.
02-01 22:22:07.180 23428-23428/? E/SQLiteLog: (1) AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY
02-01 22:22:07.200 23428-23428/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x42020540)
02-01 22:22:07.380 23428-23428/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
at android.view.View.performClick(View.java:4102)
at android.view.View$PerformClick.run(View.java:17085)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5520)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:4102)
at android.view.View$PerformClick.run(View.java:17085)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5520)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY (code 1): , while compiling: CREATE TABLE Logs(_idINTEGER PRIMARY KEY AUTOINCREMENT,titleTEXT,plate_numberTEXT,sort_idTEXT,gradeTEXT,diameterTEXT,lengthTEXT,survey_idINTEGER AUTOINCREMENT);
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:909)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:520)
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:1719)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1650)
at com.example.matija.ams.LogsDBHandler.onCreate(LogsDBHandler.java:51)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.example.matija.ams.LogsDBHandler.addLogs(LogsDBHandler.java:62)
at com.example.matija.ams.AddLogs.saveButtonClicked(AddLogs.java:55)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:4102)
at android.view.View$PerformClick.run(View.java:17085)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5520)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)
02-01 22:22:09.240 23428-23428/? D/Process: killProcess, pid=23428
02-01 22:22:09.270 23428-23428/? D/Process: dalvik.system.VMStack.getThreadStackTrace(Native Method)
02-01 22:22:09.270 23428-23428/? D/Process: java.lang.Thread.getStackTrace(Thread.java:599)
02-01 22:22:09.270 23428-23428/? D/Process: android.os.Process.killProcess(Process.java:956)
02-01 22:22:09.270 23428-23428/? D/Process: com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:108)
02-01 22:22:09.270 23428-23428/? D/Process: java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
02-01 22:22:09.270 23428-23428/? D/Process: java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
02-01 22:22:09.270 23428-23428/? D/Process: dalvik.system.NativeStart.main(Native Method)
And this is my DbHandler with add method
public class LogsDBHandler extends SQLiteOpenHelper {
//DB version
private static final int DATABASE_VERSION = 11;
// DB name
private static final String DATABASE_NAME = "Log";
//table name
public static final String TABLE_LOGS = "Logs";
public static final String TABLE_SURVEY = "SURVEY";
//TableLogs column names
public static final String KEY_ID = "_id";
public static final String KEY_TITLE = "title";
public static final String KEY_PLATE_NUMBER = "plate_number";
public static final String KEY_SORTID = "sort_id";
public static final String KEY_GRADE = "grade";
public static final String KEY_DIAMETER = "diameter";
public static final String KEY_LENGTH = "length";
public static final String KEY_SURVEYID = "survey_id";
//TableSurvey column names
public static final String KEY_CREATEDAT = "created_at";
public static final String KEY_SURVEY_TITLE = "survey_title";
public static final String KEY_STATE = "state";
public LogsDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LOGS_TABLE = "CREATE TABLE " + TABLE_LOGS + "(" +
KEY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," +
KEY_TITLE + "TEXT," + KEY_PLATE_NUMBER + "TEXT," +
KEY_SORTID + "TEXT," + KEY_GRADE + "TEXT," +
KEY_DIAMETER + "TEXT," + KEY_LENGTH + "TEXT," +
KEY_SURVEYID + "INTEGER AUTOINCREMENT" +");";
db.execSQL(CREATE_LOGS_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGS);
onCreate(db);
}
//add new row
public void addLogs(Logs log) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, log.get_title());
values.put(KEY_PLATE_NUMBER, log.get_plate_number());
values.put(KEY_SORTID, log.get_sort_id());
values.put(KEY_GRADE, log.get_grade());
values.put(KEY_DIAMETER, log.get_diameter());
values.put(KEY_LENGTH, log.get_length());
db.insert(TABLE_LOGS, null, values);
db.close();
}
}
So next code is for adding data from edittext to database. If anything is wrong please tell me.
public class AddLogs extends AppCompatActivity {
private EditText title;
private EditText plate_number;
private Spinner sort_id;
private Spinner grade;
private EditText diameter;
private EditText length;
LogsDBHandler dbHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_logs);
final Button changeActivityButton1 = (Button) findViewById(R.id.btn_back);
changeActivityButton1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View aView) {
Intent toAnotherActivity = new Intent(aView.getContext(), MainActivity.class);
startActivityForResult(toAnotherActivity, 0);
}
});
title = (EditText) findViewById(R.id.inputTitle);
plate_number = (EditText) findViewById(R.id.supplierNumberInput);
sort_id = (Spinner) findViewById(R.id.spinnerSortInput);
grade = (Spinner) findViewById(R.id.spinnerClassInput);
diameter = (EditText) findViewById(R.id.diameterInput);
length = (EditText) findViewById(R.id.lengthInput);
dbHandler = new LogsDBHandler(this, null, null, 1);
}
public void saveButtonClicked(View view) {
Logs log = new Logs(title.getText().toString(), plate_number.getText().toString(),
sort_id.getSelectedItem().toString(), grade.getSelectedItem().toString(),
diameter.getText().toString(), length.getText().toString());
dbHandler.addLogs(log);
Toast.makeText(getBaseContext(), "Spremljeno", Toast.LENGTH_SHORT ).show();
}
}
So this is all code (i didn't put the code with constructors, getters and setters). So i anybody knows what is the problem please say and write. Thanks.
Your SQL is not formatted correctly. You don't have spaces between the column names and the rest of their properties. That's why the exception root cause is:
Caused by: android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY (code 1): , while compiling: CREATE TABLE Logs(_idINTEGER PRIMARY KEY AUTOINCREMENT,titleTEXT,plate_numberTEXT,sort_idTEXT,gradeTEXT,diameterTEXT,lengthTEXT,survey_idINTEGER AUTOINCREMENT);
Take a look at your onCreate callback:
String CREATE_LOGS_TABLE = "CREATE TABLE " + TABLE_LOGS + "(" +
KEY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," +
KEY_TITLE + "TEXT," + KEY_PLATE_NUMBER + "TEXT," +
KEY_SORTID + "TEXT," + KEY_GRADE + "TEXT," +
KEY_DIAMETER + "TEXT," + KEY_LENGTH + "TEXT," +
KEY_SURVEYID + "INTEGER AUTOINCREMENT" +");";
If you are going to manually build the SQL string, instead of using a migration library such as Flyway, you need to ensure your SQL is formatted according to the standard.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I'm not familiar with SQlite so I'm not sure if I'm passing incorrect data type to the DBHelper class or what I'm doing wrong.
This is my logcat
04-16 22:31:07.341 21052-21052/com.example.android.inputtest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.inputtest, PID: 21052
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: android.database.sqlite.SQLiteException: near "jordytip": syntax error (code 1): , while compiling: CREATE TABLE Tips(_id INTEGER PRIMARY KEY AUTOINCREMENT jordytip INTEGER mariatip INTEGER )
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1795)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1726)
at com.example.android.inputtest.MyDBHandler.onCreate(MyDBHandler.java:32)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.android.inputtest.MyDBHandler.addJordyTip(MyDBHandler.java:44)
at com.example.android.inputtest.MainActivity.submit(MainActivity.java:50)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
This is my main activity:
package com.example.android.inputtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int jordyTips = 388;
int mariaTips = 388;
int more;
int less;
EditText tipInput;
TextView jordyOutput;
MyDBHandler dbHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tipInput = (EditText) findViewById(R.id.tipInput);
jordyOutput = (TextView) findViewById(R.id.jordyOutput);
dbHandler = new MyDBHandler(this,null, null,1);
}
public void submit (View view){
String value= tipInput.getText().toString();
int newTip =Integer.parseInt(value);
if (newTip % 2 == 0){
more = newTip/2;
less = newTip/2;
} else {
more = newTip/2 + 1;
less = newTip/2;
}
if (jordyTips <= mariaTips) {
jordyTips += more;
TipData tip = new TipData(more);
dbHandler.addJordyTip(tip);
printDatabase();
mariaTips += less;
} else {
jordyTips += less;
TipData tip = new TipData(less);
dbHandler.addJordyTip(tip);
printDatabase();
mariaTips += more;}
displayQuantity();
}
public void displayQuantity() {
TextView quantityTextView1 = jordyOutput;
TextView quantityTextView2 = (TextView)findViewById(R.id.mariaOutput);
quantityTextView1.setText((jordyTips-more)+ "+" + more + " " + String.valueOf(jordyTips));
quantityTextView2.setText(String.valueOf(mariaTips)+ "+" + less);
}
public void printDatabase(){
String dbString = dbHandler.databaseToString();
jordyOutput.setText(dbString);
tipInput.setText("");
}
}
This is my TipData class:
package com.example.android.inputtest;
/**
* Created by jordy on 4/16/2017.
*/
public class TipData {
private int _id;
private int _jordytip;
public TipData(int jordytip) {
this._jordytip = jordytip;
}
public void set_id(int _id) {
this._id = _id;
}
public void set_jordytip(int _jordytip) {
this._jordytip = _jordytip;
}
public int get_id() {
return _id;
}
public int get_jordytip() {
return _jordytip;
}
}
and this is my DBHandler class:
package com.example.android.inputtest;
/**
* Created by jordy on 4/16/2017.
*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "tips.db";
public static final String TABLE_NAME = "Tips";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_JORDYTIP = "jordytip";
public static final String COLUMN_MARIATIP = "mariatip";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_NAME + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
COLUMN_JORDYTIP + " INTEGER " +
COLUMN_MARIATIP + " INTEGER " +
")";
db.execSQL(query);
}
#Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(db);
}
public void addJordyTip(TipData jordytip){
ContentValues values = new ContentValues();
values.put(COLUMN_JORDYTIP,jordytip.get_jordytip() );
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_NAME, null, values);
db.close();
}
public void deleteTip(String jordyTip){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM" + TABLE_NAME + " WHERE " + COLUMN_JORDYTIP + "=\"" + jordyTip + "\";");
}
public String databaseToString(){
String dbString= "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM" + TABLE_NAME + "WHERE 1";
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while(!c.isAfterLast()){
if (c.getString(c.getColumnIndex("jordytip"))!=null){
dbString += c.getString(c.getColumnIndex("jordytip"));
dbString += "\n";
}
}
db.close();
return dbString;
}
}
Getting this now:
04-17 00:17:58.181 20893-20893/com.example.android.inputtest E/SQLiteLog: (1) near "1": syntax error
04-17 00:17:58.191 20893-20893/com.example.android.inputtest E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.inputtest, PID: 20893
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: android.database.sqlite.SQLiteException: near "1": syntax error (code 1): , while compiling: SELECT * FROM TipsWHERE 1
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1437)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1376)
at com.example.android.inputtest.MyDBHandler.databaseToString(MyDBHandler.java:63)
at com.example.android.inputtest.MainActivity.printDatabase(MainActivity.java:83)
at com.example.android.inputtest.MainActivity.submit(MainActivity.java:51)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
However when I add the space before where
String query = "SELECT * FROM " + TABLE_NAME + " WHERE 1";
I get "app not responding".
Caused by: android.database.sqlite.SQLiteException: near "jordytip":
syntax error (code 1): , while compiling: CREATE TABLE Tips(_id
INTEGER PRIMARY KEY AUTOINCREMENT jordytip INTEGER mariatip INTEGER )
At first Rectify Query
String query = "CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,"
+ COLUMN_JORDYTIP + " INTEGER ,"
+ COLUMN_MARIATIP + " INTEGER" + ")";
All files under my SQLite package are giving errors. As these are predefined classes and don't require or allow editing, I'm having a hard time making my project.
The codes are too long to be posted here.
For instance, these imports in SQLiteDatabase.java are appearing in red:
import android.database.sqlite.SQLiteDebug.DbStats;
import dalvik.system.CloseGuard;
and my logcat shows the following errors:
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: FATAL EXCEPTION: main
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: android.database.sqlite.SQLiteException: no such table: user (code 1): , while compiling: select * from user
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at com.example.abcd.helloworld.DatabaseHelper.insertUser(DatabaseHelper.java:40)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at com.example.abcd.helloworld.SignUp$1.onClick(SignUp.java:52)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.view.View.performClick(View.java:4240)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:17721)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:730)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5103)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:525)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
All coding done by me seems to be correct.
The code for table creation is:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "user.db";
private static final String TABLE_NAME = "user";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_EMAIL = "email";
private static final String COLUMN_UNAME = "uname";
private static final String COLUMN_PASS = "pass";
SQLiteDatabase db;
private static final String TABLE_CREATE = "create table user (id integer primary key autoincrement," +
"name text, email text, uname text, pass text);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
this.db = db;
}
public void insertUser(User u) {
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "select * from user";
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount();
values.put(COLUMN_ID, count);
values.put(COLUMN_NAME, u.getName());
values.put(COLUMN_EMAIL, u.getEmail());
values.put(COLUMN_UNAME, u.getUname());
values.put(COLUMN_PASS, u.getPass());
db.insert(TABLE_NAME, null, values);
}
public String searchPass(String uname) {
db = this.getReadableDatabase();
String query = "select uname, pass from" + TABLE_NAME;
Cursor cursor = db.rawQuery(query, null);
String a, b;
b = "not found";
if (cursor.moveToFirst()) {
do {
a = cursor.getString(0);
if (a.equals(uname)) {
b = cursor.getString(1);
break;
}
}
while (cursor.moveToNext());
}
db.close();
return b;
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXISTS" + TABLE_NAME;
db.execSQL(query);
this.OnCreate(db);
}
private void OnCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
this.db = db;
}
}
You are using onCreate method twice in your code. Remove duplication.
Try below code:
public class DBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "user.db";
public DBHandler(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
String query = "create table user(" +
"id integer primary key autoincrement, " +
"name text, "+
"email text, "+
"uname text, "+
"pass text"+
");";
db.execSQL(query);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "drop table if exists user";
db.execSQL(query);
onCreate(db);
}
/*
Remaining code
Please do not add onCreate and onUpgrade method anymore.
*/
}
Your code seems to be ok, try to debug the SQLite code. Execute the statement outside the android code. Open your terminal and execute the sql statement, and after that execute .tables, this command will show you the existed tables in your db.
More info, you can find here,
http://www.tutorialspoint.com/sqlite/sqlite_create_table.htm
I am having an issue with my college project. I am trying to delete a row from my one of my tables using the following query.
//---deletes a particular match---
public boolean deleteMatch(String name) {
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
return sqLiteDatabase.delete(TABLE_FIXTURES, MATCH_OPPONENT + " = " + name, null) > 0;
}
Here I am trying to delete a record based upon the match_opponent and passing the String value name.
I am then calling this method in my EditSchedule activity below:
public class EditSchedule extends AppCompatActivity {
public Button fixtureSearch;
public EditText opponentName;
public String searchTerm;
ListView editMatch;
DBHelper dbHelper = new DBHelper(this);
SQLiteDatabase sqLiteDatabase;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editfixture);
editMatch = (ListView) findViewById(R.id.listViewEditMatch);
opponentName = (EditText) findViewById(R.id.fixtureOpponentDelete);
searchTerm = opponentName.getText().toString();
fixtureSearch = (Button) findViewById(R.id.fixtureSearchButton);
fixtureSearch.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
dbHelper.deleteMatch(searchTerm);
}
}
);
}
}
So when I test my application by typing in a name of an opponent that I have in the fixture table and clicking on the delete button, I get the following error:
01-06 07:49:42.476 25778-25778/com.example.myacer.clubhub E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: DELETE FROM fixtures WHERE match_opponent =
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
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.delete(SQLiteDatabase.java:1494)
at com.example.myacer.clubhub.database.DBHelper.deleteMatch(DBHelper.java:243)
at com.example.myacer.clubhub.manager.EditSchedule$1.onClick(EditSchedule.java:47)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17721)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Can anyone see where I am going wrong? All help greatly appreciated.
You are passing blank value in deleteMatch(), put searchTerm = opponentName.getText().toString(); inside onClick()
fixtureSearch.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
searchTerm = opponentName.getText().toString();
dbHelper.deleteMatch(searchTerm);
}
}
);
As well as change your deleteMatch() method
sqLiteDatabase.delete(TABLE_FIXTURES, MATCH_OPPONENT + " = ?",new String[]{name});
try this:
sqLiteDatabase.delete(TABLE_FIXTURES, MATCH_OPPONENT + " = ?", new String[]{name})
My app closes when i insert this sample records for testing, i tried it in many ways. from activity class im sending string values to insert to the database.
My MainInvoiceActivity Class
public class MainInvoiceActivity extends Activity {
private DBHelper mydb ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_invoice);
if ( mydb.insertsample( "one" , "two" , "3" )){ // LINE NO 67
Toast.makeText(getApplicationContext(), "Insert Success!", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_SHORT).show();
}
}
}
My database SQLiteOpenHelper class
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyDBName.db";
public static final String TABLE_SAMPLE = "sample";
public DBHelper(Context context)
{
super(context, DATABASE_NAME , null, 1);
}
public void onCreate(SQLiteDatabase db) {
String CREATE_SAMPLE_TABLE = "CREATE TABLE sample ( " +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"one TEXT, "+
"two TEXT, "+
"three TEXT )";
db.execSQL(CREATE_SAMPLE_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS sample");
onCreate(db);
}
public boolean insertsample (String one, String two, String three)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues myValues = new ContentValues();
myValues.put(one, one);
myValues.put(two, two);
myValues.put(three, three);
db.insert(TABLE_SAMPLE, null, myValues);
return true;
}
}
LOG message
E/AndroidRuntime(1153): FATAL EXCEPTION: main
E/AndroidRuntime(1153): Process: com.ezycode.pos, PID: 1153
E/AndroidRuntime(1153): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ezycode.pos/com.ezycode.pos.MainInvoiceActivity}: java.lang.NullPointerException
E/AndroidRuntime(1153): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
E/AndroidRuntime(1153): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
E/AndroidRuntime(1153): at android.app.ActivityThread.access$800(ActivityThread.java:135)
E/AndroidRuntime(1153): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
E/AndroidRuntime(1153): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(1153): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(1153): at android.app.ActivityThread.main(ActivityThread.java:5017)
E/AndroidRuntime(1153): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1153): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(1153): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
E/AndroidRuntime(1153): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
E/AndroidRuntime(1153): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(1153): Caused by: java.lang.NullPointerException
E/AndroidRuntime(1153): at com.ezycode.pos.MainInvoiceActivity.onCreate(MainInvoiceActivity.java:67)
E/AndroidRuntime(1153): at android.app.Activity.performCreate(Activity.java:5231)
E/AndroidRuntime(1153): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime(1153): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
Correct code will be like something below
public class MainInvoiceActivity extends Activity {
private DBHelper mydb ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_invoice);
mydb = new DBHelper(this); //This is missing your code.
if ( mydb.insertsample( "one" , "two" , "3" )){ // LINE NO 67
Toast.makeText(getApplicationContext(), "Insert Success!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "not done", Toast.LENGTH_SHORT).show();
}
}
I am working on a simple app for myself that posts information to a database. I'm really new at this and tried to follow some tutorials along with other tidbits to assemble this.
There is a home screen that gets to the second screen which has a button (add item). Onclicking, this is supposed to build/update the database. I get a crash when I enter that second screen before anything happens. I tried to put in breakpoints to debug but I can't even to get anywhere. It doesn't pause at my breakpoints (or doesn't seem to) so I can't see what's going on
Can anyone point in the right direction for debugging/fixing this?
This is my 2nd screen code - java
public class AddItem extends Activity {
private final String TAG = "Main Activity";
View view;
SQLiteDatabase db;
DbPrice dbprice ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_item);
Log.i(TAG, "OnCreate");
dbprice = new DbPrice(this);
db = dbprice.getWritableDatabase();
Button addButton = (Button) findViewById(R.id.addNew);
addButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String subcat,item,store,extra;
Integer day,month,year,price,quantity,weight,volume;
Boolean sale;
DatePicker datePicker1 = (DatePicker) findViewById(R.id.datePicker1);
AutoCompleteTextView autoCompleteSubCat = (AutoCompleteTextView) findViewById(R.id.autoCompleteSubCat);
EditText editItem = (EditText) findViewById(R.id.editItem);
EditText editPrice = (EditText) findViewById(R.id.editPrice);
EditText editQuantity = (EditText) findViewById(R.id.editQuantity);
EditText editWeight = (EditText) findViewById(R.id.editWeight);
EditText editVolume = (EditText) findViewById(R.id.editVolume);
CheckBox checkSale = (CheckBox) findViewById(R.id.checkSale);
AutoCompleteTextView autoCompleteStore = (AutoCompleteTextView) findViewById(R.id.autoCompleteStore);
EditText editExtra = (EditText) findViewById(R.id.editExtra);
day = datePicker1.getDayOfMonth();
month = datePicker1.getMonth();
year = datePicker1.getYear();
subcat = autoCompleteSubCat.getText().toString();
item = editItem.getText().toString();
extra = editExtra.getText().toString();
price = Integer.parseInt(editPrice.getText().toString());
quantity = Integer.parseInt(editQuantity.getText().toString());
weight = Integer.parseInt(editWeight.getText().toString());
volume = Integer.parseInt(editVolume.getText().toString());
// sale = checkSale.isChecked();
store = autoCompleteStore.getText().toString();
ContentValues cv = new ContentValues();
cv.put(DbPrice.SUBCAT, subcat);
cv.put(DbPrice.ITEM, item);
cv.put(DbPrice.EXTRA, extra);
cv.put(DbPrice.PRICE, price);
cv.put(DbPrice.QUANTITY, quantity);
cv.put(DbPrice.WEIGHT, weight);
cv.put(DbPrice.VOLUME, volume);
cv.put(DbPrice.SALE, sale);
cv.put(DbPrice.STORE, store);
db.insert(DbPrice.TABLE_NAME, null, cv);
}
});
}
#Override
public void onStart() {
super.onStart();
Log.i(TAG, "OnStart");
}
#Override
public void onResume() {
super.onResume();
Log.i(TAG, "OnResume");
}
public void OnPause() {
super.onPause();
Log.i(TAG,"OnPause");
}
public void OnStop() {
super.onStart();
Log.i(TAG, "OnStop");
}
public void OnDestroy() {
super.onDestroy();
Log.i(TAG, "OnDestroy");
}
public void addNewItem (View v) {
Log.i(TAG, "Starting New Activity");
Intent intent = new Intent (this, AllItems.class);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add_item, menu);
return true;
}
}
The database class java is below. I don't know if you need the xml too but I included the catlog.
public class DbPrice extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "data";
public static final String TABLE_NAME = "price_table";
public static final String C_ID = "_id";
public static final String DAY = "day";
public static final String MONTH = "month";
public static final String YEAR = "day";
public static final String SUBCAT = "subcategory";
public static final String ITEM = "item";
public static final String PRICE = "price";
public static final String QUANTITY = "quantity";
public static final String WEIGHT = "weight";
public static final String VOLUME = "volume";
public static final String SALE = "sale";
public static final String STORE = "store";
public static final String EXTRA = "extra";
public static final int VERSION = 1;
private final String createDb = "create table if not exists " + TABLE_NAME+ " ( "
+ C_ID + " integer primary key autoincrement, "
+ DAY + " text, "
+ MONTH + " text, "
+ YEAR + " text, "
+ SUBCAT + " text, "
+ ITEM + " text, "
+ PRICE + " text, "
+ QUANTITY + " text, "
+ WEIGHT + " text, "
+ VOLUME + " text, "
+ SALE + " text, "
+ STORE + " text, "
+ EXTRA + " text) ";
public DbPrice(Context context) {
super(context, DATABASE_NAME, null, VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(createDb);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
db.execSQL("drop table " + TABLE_NAME);
}
}
CATLOG BELOW
12-29 01:27:20.834: I/Main Activity(1362): OnCreate
12-29 01:27:21.044: E/SQLiteLog(1362): (1) duplicate column name: day
12-29 01:27:21.054: D/AndroidRuntime(1362): Shutting down VM
12-29 01:27:21.064: W/dalvikvm(1362): threadid=1: thread exiting with uncaught exception (group=0xb4b11b90)
12-29 01:27:21.194: E/AndroidRuntime(1362): FATAL EXCEPTION: main
12-29 01:27:21.194: E/AndroidRuntime(1362): Process: com.unsuccessfulstudent.grocerypricehistory, PID: 1362
12-29 01:27:21.194: E/AndroidRuntime(1362): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.unsuccessfulstudent.grocerypricehistory/com.unsuccessfulstudent.grocerypricehistory.AddItem}: android.database.sqlite.SQLiteException: duplicate column name: day (code 1): , while compiling: create table if not exists price_table ( _id integer primary key autoincrement, day text, month text, day text, subcategory text, item text, price text, quantity text, weight text, volume text, sale text, store text, extra text)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.app.ActivityThread.access$700(ActivityThread.java:135)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.os.Handler.dispatchMessage(Handler.java:102)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.os.Looper.loop(Looper.java:137)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.app.ActivityThread.main(ActivityThread.java:4998)
12-29 01:27:21.194: E/AndroidRuntime(1362): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 01:27:21.194: E/AndroidRuntime(1362): at java.lang.reflect.Method.invoke(Method.java:515)
12-29 01:27:21.194: E/AndroidRuntime(1362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
12-29 01:27:21.194: E/AndroidRuntime(1362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
12-29 01:27:21.194: E/AndroidRuntime(1362): at dalvik.system.NativeStart.main(Native Method)
12-29 01:27:21.194: E/AndroidRuntime(1362): Caused by: android.database.sqlite.SQLiteException: duplicate column name: day (code 1): , while compiling: create table if not exists price_table ( _id integer primary key autoincrement, day text, month text, day text, subcategory text, item text, price text, quantity text, weight text, volume text, sale text, store text, extra text)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
12-29 01:27:21.194: E/AndroidRuntime(1362): at com.unsuccessfulstudent.grocerypricehistory.DbPrice.onCreate(DbPrice.java:49)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
12-29 01:27:21.194: E/AndroidRuntime(1362): at com.unsuccessfulstudent.grocerypricehistory.AddItem.onCreate(AddItem.java:31)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.app.Activity.performCreate(Activity.java:5243)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-29 01:27:21.194: E/AndroidRuntime(1362): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
12-29 01:27:21.194: E/AndroidRuntime(1362): ... 11 more
12-29 01:27:24.554: I/Process(1362): Sending signal. PID: 1362 SIG: 9
The reason for your app crash is caused by the fact that in your database creation query you are defining the same key twice. Thats why you are getting a duplicate column error.
Both the keys you have,i.e., DAY as well as YEAR have the same value day. You cannot have two columns in a database with the same name. So, to resolve this, all you need to do is change the YEAR definition to-
public static final String YEAR = "year";
Hope this helps!!!
You use two same columns
DAY = "day";
YEAR = "day";
Code:
create table if not exists price_table
(
_id integer primary key autoincrement,
"**day**" text,
month text,
"**day**" text,...
)
Whenever there is an Exception, if you see a line FATAL EXCEPTION: main in Logcat, look at the below lines. The line with Unable to start activity ComponentInfo, will give you a clear idea about the cause of exception. Still if you are confused, look for a line starting with Caused by:.
So here your problem is "duplicate column name: day".
Also you can navigate to the specific line which caused the exception. Just look for your package name. Here in this case,
12-29 01:27:21.194: E/AndroidRuntime(1362): at com.unsuccessfulstudent.grocerypricehistory.DbPrice.onCreate(DbPrice.java:49)
12-29 01:27:21.194: E/AndroidRuntime(1362): at com.unsuccessfulstudent.grocerypricehistory.AddItem.onCreate(AddItem.java:31)
At the end of the lines, There is corresponding class and line number. Note that these are the lines caused exception, but the cause of exception may be somewhere else. In this case, String YEAR