Android Studio Application crash on database insertion - java

When I call function 'test' my app crashes. Any ideas why does that happen?
DB class
package com.example.paulcosma.app2;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DB extends SQLiteOpenHelper{
public static final String
dbName = "foods.db",
tableName = "foods",
colId = "id",colName = "name",colCarbs = "carbs";
public DB(Context context) {
super(context, dbName, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + dbName + "( " + colId + " INTEGER PRIMARY KEY AUTOINCREMENT," + colName + " TEXT," + colCarbs + " REAL)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + dbName);
onCreate(db);
}
public boolean addFood(String Name, float Carbs){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(colName,Name);
contentValues.put(colCarbs,Carbs);
long result = db.insert(tableName,null,contentValues);
return result != -1; // -1 == not inserted
}
}
MainActivity
package com.example.paulcosma.app2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
DB db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DB(this);
}
public void test(View v){
boolean insertedSuccessfully = db.addFood("Milk",4); // crashes when called
if(insertedSuccessfully)
Toast.makeText(this,"inserted",Toast.LENGTH_LONG).show();
else Toast.makeText(this,"not inserted",Toast.LENGTH_LONG).show();
}
}
logcat
01-20 19:59:47.888 9565-9565/com.example.paulcosma.app2 E/InstantRun:
Could not find slices in APK; aborting. 01-20 19:59:47.988
9565-9565/com.example.paulcosma.app2 E/dalvikvm: Could not find class
'android.graphics.drawable.RippleDrawable', referenced from method
android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
01-20 19:59:48.005 9565-9565/com.example.paulcosma.app2 E/TextView:
zhangcl: get font resource from application failed. 01-20
19:59:48.013 9565-9565/com.example.paulcosma.app2 E/TextView: zhangcl:
get font resource from application failed -- 2. 01-20 19:59:48.045
9565-9565/com.example.paulcosma.app2 E/TextView: zhangcl: get font
resource from application failed. 01-20 20:00:02.211
9565-9565/com.example.paulcosma.app2 E/SQLiteLog: (10) Failed to do
file read, got: 0, amt: 100, last Errno: 2 01-20 20:00:02.244
9565-9565/com.example.paulcosma.app2 E/SQLiteLog: (1) unknown database
foods 01-20 20:00:02.263 9565-9565/com.example.paulcosma.app2
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.paulcosma.app2, PID: 9565
java.lang.IllegalStateException: Could not execute method for
android:onClick

You've got a typo in onCreate(). You're using dbName instead of tableName to create your table, therefore when you're running your query which is looking up the correct name, you're looking up a table that doesn't exist. So:
db.execSQL("create table " + dbName + "( " + colId + " INTEGER PRIMARY KEY AUTOINCREMENT," + colName + " TEXT," + colCarbs + " REAL)");
needs to become:
db.execSQL("create table " + tableName + "( " + colId + " INTEGER PRIMARY KEY AUTOINCREMENT," + colName + " TEXT," + colCarbs + " REAL)");

Related

I created app for local sqlite database but it not running. Please tell me the solution

This is DatabaseHelper.java class, in this class I write query for SQLite Database. I think there is not any problem in query.
package com.example.ankurnamikaze.myapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import static android.R.attr.id;
import static android.R.attr.name;
public class DatabaseHelper extends SQLiteOpenHelper {
SQLiteDatabase db;
public static final String DATABASE_NAME = "notes_db";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "notes";
public static final String COL_ID = "id";
public static final String COL_NAME = "name";
public static final String COL_PASS = "pass";
This method is used for creating Database.
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (" +COL_ID + " integer autoincrement," + COL_NAME + " text," + COL_PASS+ " text)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + TABLE_NAME);
onCreate(db);
}
// INSERTING ROW IN DATABASE
public void insertData(int id, String name, String pass){
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_ID, id);
values.put(COL_NAME, name);
values.put(COL_PASS, pass);
db.insert(TABLE_NAME, null, values);
}
}
This is MainActivity.java class. I think I'm getting problem in defining the DatabaseHelper class because before when i defined this outside the onCreate method it make app crash then i saw some code which tell me to define it inside the onCreate method but still it didnt work for me.
package com.example.ankurnamikaze.myapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import static android.R.attr.id;
import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
public class MainActivity extends AppCompatActivity {
DatabaseHelper databaseHelper;
EditText name, password;
String user_name, user_pass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
databaseHelper = new DatabaseHelper(this);
setContentView(R.layout.activity_main);
name = (EditText)findViewById(R.id.user_name);
password = (EditText)findViewById(R.id.user_pass);
user_name = String.valueOf(name.getText());
user_pass = String.valueOf(password.getText());
databaseHelper.insertData(id, user_name, user_pass);
}
}
This is activiy_main.xml, in this I create the attributes. And I don't think this is the source of problem.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.ankurnamikaze.myapp.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="USERNAME"
android:id="#+id/user_name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="PASSWORD"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="sumbit"
android:id="#+id/button" />
</LinearLayout>
There are a few errors/issues.
Issue 1
You have coded " integer autoincrement," this will result in a syntax error as the AUTOINCREMENT keyword can only be used with INTEGER PRIMARY KEY (saying that it's not recommended to use it as INTEGER PRIMARY KEY is more efficient and has very much the same result, that is automatically generating a unique id (actually it only makes the id an alias of the rowid in either situation)).SQLite Autoincrement
e.g.
The AUTOINCREMENT keyword imposes extra CPU, memory, disk space, and
disk I/O overhead and should be avoided if not strictly needed. It is
usually not needed
The error would result in something along the lines of the following :-
09-19 07:32:10.040 7200-7200/? E/SQLiteLog: (1) near "autoincrement": syntax error
09-19 07:32:10.040 7200-7200/? D/AndroidRuntime: Shutting down VM
09-19 07:32:10.040 7200-7200/? W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa62e9288)
09-19 07:32:10.044 7200-7200/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{so52399661.so52399661/so52399661.so52399661.MainActivity}: android.database.sqlite.SQLiteException: near "autoincrement": syntax error (code 1): , while compiling: create table notes (id integer autoincrement,name text,pass text)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
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:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near "autoincrement": syntax error (code 1): , while compiling: create table notes (id integer autoincrement,name text,pass text)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
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:1663)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
at so52399661.so52399661.DatabaseHelper.onCreate(DatabaseHelper.java:23)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at so52399661.so52399661.DatabaseHelper.insertData(DatabaseHelper.java:38)
at so52399661.so52399661.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
at android.app.ActivityThread.access$600(ActivityThread.java:130) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:4745) 
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:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
at dalvik.system.NativeStart.main(Native Method) 
Instead I'd suggest just coding :-
db.execSQL("create table " + TABLE_NAME + " (" +COL_ID + " INTEGER PRIMARY KEY," + COL_NAME + " TEXT," + COL_PASS+ " TEXT)");
Issue 2
You have no id user_pass defined in the layout.
Issue 3
id cannot be resolved (see below this isn't needed).
Solution
The following is the fixed code (see comments) with some additional code to list the tables in the Database :-
activity_main.xml (id user_pass added to PASSWORD EditText) :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="USERNAME"
android:id="#+id/user_name"/>
<EditText
android:id="#+id/user_pass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="PASSWORD"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="sumbit"
android:id="#+id/button" />
</LinearLayout>
DatabaseHelper.java (see comments) :-
public class DatabaseHelper extends SQLiteOpenHelper {
SQLiteDatabase db;
public static final String DATABASE_NAME = "notes_db";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "notes";
public static final String COL_ID = "id";
public static final String COL_NAME = "name";
public static final String COL_PASS = "pass";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
//db.execSQL("create table " + TABLE_NAME + " (" +COL_ID + " integer autoincrement," + COL_NAME + " text," + COL_PASS+ " text)"); //<<<<<<<<<< ERROR
db.execSQL("create table " + TABLE_NAME + " (" +COL_ID + " INTEGER PRIMARY KEY," + COL_NAME + " TEXT," + COL_PASS+ " TEXT)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + TABLE_NAME);
onCreate(db);
}
// INSERTING ROW IN DATABASE
//<<<<<<<<<< as INTEGER PRIMARY KEY then id will be automatically generated
public void insertData(String name, String pass){
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
//values.put(COL_ID, id); not needed will autogenerate
values.put(COL_NAME, name);
values.put(COL_PASS, pass);
db.insert(TABLE_NAME, null, values);
}
}
MainActivity.java (see comments) :-
public class MainActivity extends AppCompatActivity {
DatabaseHelper databaseHelper;
EditText name, password;
String user_name, user_pass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
databaseHelper = new DatabaseHelper(this);
setContentView(R.layout.activity_main);
name = (EditText)findViewById(R.id.user_name);
password = (EditText)findViewById(R.id.user_pass);
user_name = String.valueOf(name.getText());
user_pass = String.valueOf(password.getText());
//databaseHelper.insertData(id, user_name, user_pass); //<<<<<<<<<< got rid of id
databaseHelper.insertData(user_name, user_pass);
//<<<<<<<<<< ADDED to List the tables
Cursor csr = databaseHelper.getWritableDatabase().query("sqlite_master",null,null,null,null,null,null );
while (csr.moveToNext()) {
Log.d("TABLES","Found table " + csr.getString(csr.getColumnIndex("name")));
}
csr.close();
}
}
Result in the Log :-
09-19 07:10:20.628 6977-6977/so52399661.so52399661 D/TABLES: Found table android_metadata
Found table notes
i.e. you notes table exists, android_metedata is a table created by the SQLiteOpenhelper class that you DatabaseHelper is a subclass of. It contains the locale.

android.database.sqlite.SQLiteException: no such table: ANECDOTAS (code 1): , while compiling: SELECT * FROM ANECDOTAS [duplicate]

This question already has answers here:
When does SQLiteOpenHelper onCreate() / onUpgrade() run?
(15 answers)
Closed 4 years ago.
hi i´m trying to create a table but it doesn´t work here is my code:
public class AnecdotasSQLiteHelper extends SQLiteOpenHelper {
static final String DATABASE_NAME = "AnecdotasDB";
static final int DATABASE_VERSION = 1;
ArrayList<Anecdota> listaAnecdotas;
static final String CREATE_TABLE_ANECDOTAS =
"CREATE TABLE "+ AnecdotasDBContract.Entry.TABLE_NAME +
"( "+ AnecdotasDBContract.Entry.COLUMN_TITULO +
" TEXT NOT NULL ,"+
AnecdotasDBContract.Entry.COLUMN_ANECDOTA +
" TEXT NOT NULL)";
public AnecdotasSQLiteHelper(Context contexto) {
super(contexto, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_ANECDOTAS);
cargaInicial(db);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " +
AnecdotasDBContract.Entry.TABLE_NAME);
onCreate(db);
}
}
AnecdotasDBContract
public class AnecdotasDBContract {
public static abstract class Entry {
public static final String TABLE_NAME = "ANECDOTAS";
public static final String COLUMN_TITULO = "TITULO";
public static final String COLUMN_ANECDOTA = "ANECDOTA";
}
}
in other projects that i have created all run perfectly but i don´t understanda why i have this error
03-16 14:27:38.834 15171-15171/com.example.mario.especial50 E/SQLiteLog: (1) no such table: ANECDOTAS
03-16 14:27:38.835 15171-15171/com.example.mario.especial50 D/AndroidRuntime: Shutting down VM
03-16 14:27:38.837 15171-15171/com.example.mario.especial50 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mario.especial50, PID: 15171
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mario.especial50/com.example.mario.especial50.Anecdotas}: android.database.sqlite.SQLiteException: no such table: ANECDOTAS (code 1): , while compiling: SELECT * FROM ANECDOTAS
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2906)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1605)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:172)
at android.app.ActivityThread.main(ActivityThread.java:6637)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.database.sqlite.SQLiteException: no such table: ANECDOTAS (code 1): , while compiling: SELECT * FROM ANECDOTAS
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.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1257)
at baseDatos.AnecdotasDataSource.leerAnecdotas(AnecdotasDataSource.java:49)
at com.example.mario.especial50.Anecdotas.cargarLista(Anecdotas.java:51)
at com.example.mario.especial50.Anecdotas.onCreate(Anecdotas.java:38)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2784)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2906)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1605)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:172)
at android.app.ActivityThread.main(ActivityThread.java:6637)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
try this
package com.historyplay.sanjosehills.util;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class AnecdotasSQLiteHelper extends SQLiteOpenHelper {
static final String DATABASE_NAME = "AnecdotasDB";
static final int DATABASE_VERSION = 1;
ArrayList<Anecdota> listaAnecdotas;
static final String CREATE_TABLE_ANECDOTAS =
"CREATE TABLE "+ AnecdotasDBContract.Entry.TABLE_NAME +
"( "+ AnecdotasDBContract.Entry.COLUMN_TITULO +
" TEXT NOT NULL ,"+
AnecdotasDBContract.Entry.COLUMN_ANECDOTA +
" TEXT NOT NULL)";
SQLiteDatabase db;
public AnecdotasSQLiteHelper(Context contexto) {
super(contexto, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_ANECDOTAS);
this.db = db;
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " +
AnecdotasDBContract.Entry.TABLE_NAME);
onCreate(db);
}
public void cargaInicial(){
// not receive db as parameter, then use this.db
.... your code
}
}
and call the class outside where execute the getWritableDatabase method
AnecdotasSQLiteHelper store = new AnecdotasSQLiteHelper(this.getContext(), "ANECDOTASDB", null, 1);
store.getWritableDatabase();
store.cargaInicial();

SQLite Database Error Newbie

This is my first SQLite App and I found a lot of errors and I don't what to do.
Can anyone tell me what I need to do?
This is my log cat:
12-26 18:58:05.870 26185-26185/? I/art: Late-enabling -Xcheck:jni
12-26 18:58:05.889 26185-26185/? E/Environment: initForCurrentUser:userId= 0
12-26 18:58:05.889 26185-26185/? D/Environment: UserEnvironment current
userId IS : 0
12-26 18:58:05.889 26185-26185/? D/Environment: UserEnvironment PRIMARY
STORAGE IS : MEDIA_INTERNAL
12-26 18:58:05.982 26185-26185/com.example.acer.sqlitecrud I/LoadedApk: No
resource references to update in package com.hmct.hmcttheme
12-26 18:58:06.010 26185-26185/com.example.acer.sqlitecrud W/art: Before
Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-26 18:58:06.135 26185-26185/com.example.acer.sqlitecrud E/SQLiteLog: (1)
near "TABLEstudentTable": syntax error
12-26 18:58:06.136 26185-26185/com.example.acer.sqlitecrud D/AndroidRuntime:
Shutting down VM
12-26 18:58:06.138 26185-26185/com.example.acer.sqlitecrud E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.example.acer.sqlitecrud, PID: 26185
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.acer.sqlitecrud/com.example.acer.sqlitecrud.MainActivity}: android.database.sqlite.SQLiteException: near "TABLEstudentTable": syntax error (code 1): , while compiling: CREATE TABLEstudentTable(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, MARKS INTEGER)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2394)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1306)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5270)
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:913)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:708)
Caused by: android.database.sqlite.SQLiteException: near "TABLEstudentTable": syntax error (code 1): , while compiling: CREATE TABLEstudentTable(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, MARKS INTEGER)
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.executeSql(SQLiteDatabase.java:1674)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at com.example.acer.sqlitecrud.DatabaseHelper.onCreate(DatabaseHelper.java:27)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.acer.sqlitecrud.DatabaseHelper.<init>(DatabaseHelper.java:22)
at com.example.acer.sqlitecrud.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:6075)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2281)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2394) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1306) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5270) 
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:913) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:708) 
12-26 18:58:06.249 26185-26185/com.example.acer.sqlitecrud I/Process: Sending signal. PID: 26185 SIG: 9
This is my class:
DatabaseHelper.Java
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String dbName = "Student.db";
public static final String tbName = "studentTable";
public static final String colID = "ID";
public static final String colName = "NAME";
public static final String colMarks = "MARKS";
public DatabaseHelper(Context context) {
super(context, dbName, null, 1);
SQLiteDatabase db = this.getWritableDatabase();
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE"+tbName+"(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, MARKS INTEGER)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS"+tbName);
onCreate(db);
}
public boolean insertData (String Name, String Marks){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(colName,Name);
contentValues.put(colMarks,Marks);
long result = db.insert(tbName,null,contentValues);
if (result == -1){
return false;
}else {
return true;
}
}
}
MainActivity.java:
package com.example.acer.sqlitecrud;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
EditText Name, Marks;
Button addData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
Name = (EditText)findViewById(R.id.edtNama);
Marks = (EditText)findViewById(R.id.edtNilai);
addData = (Button)findViewById(R.id.btnInsert);
InsertData();
}
private void InsertData() {
addData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isInserted = myDb.insertData(Name.getText().toString(),
Marks.getText().toString());
if (isInserted = true){
Toast.makeText(MainActivity.this,"Data Telah Di Inputkan !!",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(MainActivity.this,"Data Gagal Di Inputkan !!",Toast.LENGTH_SHORT).show();
}
}
});
}
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+tbName+"(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, MARKS INTEGER)");
}
note the space between the TABLE and tbName
Query syntax error, space is missing.
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+tbName+"(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, MARKS INTEGER)");
}
You are missing space in syntax of create table
db.execSQL("CREATE TABLE "+tbName+"(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, MARKS INTEGER)");
In drop table also you missed the space
"DROP TABLE IF EXISTS "+tbName
check below code
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+tbName+"(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, MARKS INTEGER)"); //add space after TABLE
}

Simple Android app - SQLite - SQL has stopped

I was writing an app using Android Studio. It's simple app for exercises.
I was sure it will work, but no. Something is wrong.
I will paste my code here:
package programowanie.android.sql;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
DatabaseHelper db;
EditText editTextImie;
EditText editTextNazwisko;
EditText editTextTelefon;
EditText editTextEmail;
EditText editTextUlica;
EditText editTextKod;
Button btDodaj;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DatabaseHelper(this);
editTextImie = (EditText) findViewById(R.id.editTextImie);
editTextNazwisko = (EditText) findViewById(R.id.editTextNazwisko);
editTextTelefon = (EditText) findViewById(R.id.editTextTelefon);
editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextUlica = (EditText) findViewById(R.id.editTextUlica);
editTextKod = (EditText) findViewById(R.id.editTextKod);
btDodaj = (Button) findViewById(R.id.btDodaj);
btDodaj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean czysieudalo;
czysieudalo = db.wstawdane(editTextImie.getText().toString(), editTextNazwisko.getText().toString(), editTextTelefon.getText().toString(), editTextEmail.getText().toString(), editTextUlica.getText().toString(), editTextKod.getText().toString());
if(czysieudalo){
Toast.makeText(MainActivity.this, "Udało się!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Niestety nie udało się :(!", Toast.LENGTH_SHORT).show();
}
}
});
}
}
This is my DatabaseHelper class:
package programowanie.android.sql;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Kudłaty on 2016-02-19.
*/
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String database_name ="Kontakty";
public static final String database_table ="Osoby";
public DatabaseHelper(Context context) {
super(context, database_name, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table" + database_table + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, IMIE TEXT, NAZWISKO TEXT, NUMER_TELEFONU TEXT, EMAIL TEXT, ULICA TEXT, KOD_MIASTO TEXT");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST" + database_table);
onCreate(db);
}
public boolean wstawdane(String imie, String nazwisko, String nr_tel, String email, String ulica, String kod ){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("Imie", imie);
cv.put("Nazwisko", nazwisko);
cv.put("Nr telefonu", nr_tel);
cv.put("Email", email);
cv.put("Ulica", ulica);
cv.put("Kod pocztowy", kod);
if (db.insert(database_table, null, cv)==-1){
return false;
} else {
return true;
}
}
}
I have no errors, but when I'm trying to add Record app stopping and I get "SQL has stopped"
I forgot to paste Log! Here u are!
02-19 18:02:43.119 2087-2087/programowanie.android.sql I/art: Not late-enabling -Xcheck:jni (already on)
02-19 18:02:43.195 2087-2087/programowanie.android.sql W/System: ClassLoader referenced unknown path: /data/app/programowanie.android.sql-1/lib/x86
02-19 18:02:43.348 2087-2115/programowanie.android.sql D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-19 18:02:43.410 2087-2115/programowanie.android.sql I/OpenGLRenderer: Initialized EGL, version 1.4
02-19 18:02:43.544 2087-2115/programowanie.android.sql W/EGL_emulation: eglSurfaceAttrib not implemented
02-19 18:02:43.544 2087-2115/programowanie.android.sql W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabdff300, error=EGL_SUCCESS
02-19 18:03:19.132 2087-2087/programowanie.android.sql E/SQLiteLog: (1) near "tableOsoby": syntax error
02-19 18:03:19.133 2087-2087/programowanie.android.sql D/AndroidRuntime: Shutting down VM
02-19 18:03:19.133 2087-2087/programowanie.android.sql E/AndroidRuntime: FATAL EXCEPTION: main
Process: programowanie.android.sql, PID: 2087
android.database.sqlite.SQLiteException: near "tableOsoby": syntax error (code 1): , while compiling: create tableOsoby(ID INTEGER PRIMARY KEY AUTOINCREMENT, IMIE TEXT, NAZWISKO TEXT, NUMER_TELEFONU TEXT, EMAIL TEXT, ULICA TEXT, KOD_MIASTO TEXT
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
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:1674)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at programowanie.android.sql.DatabaseHelper.onCreate(DatabaseHelper.java:21)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at programowanie.android.sql.DatabaseHelper.wstawdane(DatabaseHelper.java:31)
at programowanie.android.sql.MainActivity$1.onClick(MainActivity.java:43)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
change this
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table" + database_table + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, IMIE TEXT, NAZWISKO TEXT, NUMER_TELEFONU TEXT, EMAIL TEXT, ULICA TEXT, KOD_MIASTO TEXT");
}
to this
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + database_table + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, IMIE TEXT, NAZWISKO TEXT, NUMER_TELEFONU TEXT, EMAIL TEXT, ULICA TEXT, KOD_MIASTO TEXT);");
}
you had no spaces between 'table and (ID' so your command would have been
"create tableOsoby(ID INTEGER PRIMARY KEY AUTOINCREMENT, IMIE TEXT, NAZWISKO TEXT, NUMER_TELEFONU TEXT, EMAIL TEXT, ULICA TEXT, KOD_MIASTO TEXT"
which wont run.
Also I noticed you forgot the ) and you have forgotten the ; at the end of the statement.You must remember the ; for security reasons
Given the error log:
android.database.sqlite.SQLiteException: near "tableOsoby": syntax error (code 1): , while compiling: create tableOsoby(ID INTEGER PRIMARY KEY AUTOINCREMENT, IMIE TEXT, NAZWISKO TEXT, NUMER_TELEFONU TEXT, EMAIL TEXT, ULICA TEXT, KOD_MIASTO TEXT
I identified here two issues that probably led the SQLite to crash:
1) On the line db.execSQL("create table" + database_table + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, IMIE TEXT, NAZWISKO TEXT, NUMER_TELEFONU TEXT, EMAIL TEXT, ULICA TEXT, KOD_MIASTO TEXT"); it is missing a space between the "create table" string and the table name variable, resulting in "create tableOsoby". Same occours with the next concatenated string.
2) The closing parenthesis is missing in the string "(ID INTEGER PRIMARY KEY AUTOINCREMENT, IMIE TEXT, NAZWISKO TEXT, NUMER_TELEFONU TEXT, EMAIL TEXT, ULICA TEXT, KOD_MIASTO TEXT".
The corrected line should be:
db.execSQL("create table " + database_table + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, IMIE TEXT, NAZWISKO TEXT, NUMER_TELEFONU TEXT, EMAIL TEXT, ULICA TEXT, KOD_MIASTO TEXT)");

Having Trouble Getting SQLite Working in my Android Application

I am having a problem getting my SQLite working properly in this Android application that I'm developing. It appears as though the table isn't properly being created based on the LogCat messages. I feel like I've been staring at the screen too long. Can anyone here spot the problem in my code?
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBHelper extends SQLiteOpenHelper {
// table name
public static final String TABLE_JOKES = "jokes";
// database field names
public static final String COLUMN_ID = "_id";
public static final String COLUMN_CATEGORY = "category";
public static final String COLUMN_SUBCATEGORY = "subcategory";
public static final String COLUMN_JOKE_TYPE = "jokeType";
public static final String COLUMN_DESCRIPTION = "description";
public static final String COLUMN_QUESTION_TEXT = "questionText";
public static final String COLUMN_ANSWER_TEXT = "answerText";
public static final String COLUMN_MONOLOGUE_TEXT = "monologueText";
public static final String COLUMN_RATING_SCALE = "ratingScale";
public static final String COLUMN_COMMENTS = "comments";
public static final String COLUMN_JOKE_SOURCE = "jokeSource";
public static final String COLUMN_RELEASE_STATUS = "releaseStatus";
public static final String COLUMN_CREATED = "created";
public static final String COLUMN_MODIFIED = "modified";
private static final String DATABASE_NAME = "jokes.db"; // file name
private static final int DATABASE_VERSION = 1;
// Database creation raw SQL statement
private static final String DATABASE_CREATE = "create table " + TABLE_JOKES
+ "( " + COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_CATEGORY + " text not null, " + COLUMN_SUBCATEGORY
+ " text not null, " + COLUMN_JOKE_TYPE + " integer not null, "
+ COLUMN_DESCRIPTION + " text," + COLUMN_QUESTION_TEXT + "text,"
+ COLUMN_ANSWER_TEXT + "text," + COLUMN_MONOLOGUE_TEXT + "text,"
+ COLUMN_RATING_SCALE + "integer," + COLUMN_COMMENTS + "text,"
+ COLUMN_JOKE_SOURCE + "text," + COLUMN_RELEASE_STATUS + "integer,"
+ COLUMN_CREATED + "text," + COLUMN_MODIFIED + "text" + ");";
// static instance to share DBHelper
private static DBHelper dbHelper = null;
private SQLiteDatabase db ;
/**
* Constructor
*
* #param context
* #param name
* #param factory
* #param version
*/
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
db = getWritableDatabase();
}
/**
* This is a static method that makes sure that only one database helper
* exists across the app's lifecycle
*
* #param context
* #return
*/
public static DBHelper getDBHelper(Context context) {
if (dbHelper == null) {
dbHelper = new DBHelper(context.getApplicationContext());
}
return dbHelper;
}
/*
* (non-Javadoc)
*
* #see
* android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
* .SQLiteDatabase)
*/
#Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
/*
* (non-Javadoc)
*
* #see
* android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
* .SQLiteDatabase, int, int)
*/
#Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
Log.w(DBHelper.class.getName(), "Upgrading database from version "
+ oldVersion + " to " + newVersion);
}
/**
* CRUD - Update
*
* #param category
* #param subcategory
* #param jokeType
* #param description
* #param questionText
* #param answerText
* #param monologueText
* #param comments
* #param jokeSource
* #param ratingScale
* #param releaseStatus
* #return
*/
public long insertNewJoke(String category, String subcategory,
int jokeType, String description, String questionText,
String answerText, String monologueText, String comments,
String jokeSource, int ratingScale, int releaseStatus) {
ContentValues cv = new ContentValues();
cv.put(COLUMN_CATEGORY, category);
cv.put(COLUMN_SUBCATEGORY, subcategory);
cv.put(COLUMN_JOKE_TYPE, jokeType);
cv.put(COLUMN_DESCRIPTION, description);
cv.put(COLUMN_QUESTION_TEXT, questionText);
cv.put(COLUMN_ANSWER_TEXT, answerText);
cv.put(COLUMN_MONOLOGUE_TEXT, monologueText);
cv.put(COLUMN_COMMENTS, comments);
cv.put(COLUMN_JOKE_SOURCE, jokeSource);
cv.put(COLUMN_RATING_SCALE, ratingScale);
cv.put(COLUMN_RELEASE_STATUS, releaseStatus);
cv.put(COLUMN_CREATED, ""); // how to put date??
cv.put(COLUMN_CREATED, "");
long code = getWritableDatabase().insert(TABLE_JOKES, null, cv);
return code;
}
/**
* CRUD - Retrieve
*
* #return
*/
public Cursor getJokes() {
String[] columns = { COLUMN_CATEGORY, COLUMN_SUBCATEGORY,
COLUMN_JOKE_TYPE, COLUMN_DESCRIPTION, COLUMN_ANSWER_TEXT,
COLUMN_MONOLOGUE_TEXT, COLUMN_RATING_SCALE, COLUMN_COMMENTS,
COLUMN_JOKE_SOURCE, COLUMN_RELEASE_STATUS, COLUMN_CREATED,
COLUMN_MODIFIED }; // might need the _id column
return getWritableDatabase().query(TABLE_JOKES, columns, null, null,
null, null, null);
}
/**
* CRUD - Delete
*
* #param id
*/
public void deleteJoke(int id) {
getWritableDatabase().delete(TABLE_JOKES, COLUMN_ID + "=?",
new String[] { String.valueOf(id) });
}
}
So in onCreate() (in my MainActivity) I try to insert a new record into the database and then try to retrieve it into a cursor. However, it seems to be getting caught at the insert statement.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// the projection (fields from the database that we want to use)
String from[] = { DBHelper.COLUMN_DESCRIPTION };
// matching fields on the layout to be used with the adapter
int to[] = { R.id.tv1 };
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lvMainJokes);
dbHelper = DBHelper.getDBHelper(this);
long code = dbHelper.insertNewJoke("Holiday", "", 2, "Joke 1 Description", "", "",
"joke", "", "", 5, 1);
if (code != -1)
cursor = dbHelper.getJokes();
...
Here is the LogCat output:
11-15 15:19:08.040: I/Choreographer(1016): Skipped 45 frames! The application may be doing too much work on its main thread.
11-15 15:19:35.358: D/dalvikvm(1064): GC_FOR_ALLOC freed 58K, 4% free 2726K/2836K, paused 42ms, total 45ms
11-15 15:19:35.368: I/dalvikvm-heap(1064): Grow heap (frag case) to 3.315MB for 635812-byte allocation
11-15 15:19:35.438: D/dalvikvm(1064): GC_FOR_ALLOC freed 2K, 4% free 3344K/3460K, paused 63ms, total 63ms
11-15 15:19:35.548: E/SQLiteLog(1064): (1) table jokes has no column named releaseStatus
11-15 15:19:35.568: E/SQLiteDatabase(1064): Error inserting category=Holiday releaseStatus=1 jokeType=2 created= monologueText=joke description=Joke 1 Description subcategory= answerText= questionText= jokeSource= ratingScale=5 comments=
11-15 15:19:35.568: E/SQLiteDatabase(1064): android.database.sqlite.SQLiteException: table jokes has no column named releaseStatus (code 1): , while compiling: INSERT INTO jokes(category,releaseStatus,jokeType,created,monologueText,description,subcategory,answerText,questionText,jokeSource,ratingScale,comments) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at com.teamdotgetname.android.phase1.jokeapplication.persistence.DBHelper.insertNewJoke(DBHelper.java:143)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at com.teamdotgetname.android.phase1.jokeapplication.presentation.MainActivity.onCreate(MainActivity.java:53)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at android.app.Activity.performCreate(Activity.java:5133)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at android.os.Looper.loop(Looper.java:137)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at android.app.ActivityThread.main(ActivityThread.java:5103)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at java.lang.reflect.Method.invoke(Method.java:525)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-15 15:19:35.568: E/SQLiteDatabase(1064): at dalvik.system.NativeStart.main(Native Method)
11-15 15:19:35.798: I/Choreographer(1064): Skipped 42 frames! The application may be doing too much work on its main thread.
11-15 15:19:35.868: D/gralloc_goldfish(1064): Emulator without GPU emulation detected.
Thanks for taking a look.
At a quick glance, your code looks good. The exception:
android.database.sqlite.SQLiteException: table jokes has no column named releaseStatus (code 1)
seems erroneous because clearly you are creating the releaseStatus column in your create table DATABASE_CREATE String. I ran into a problem where I was modifying the schema of my database and the changes were not taking effect in my application because I was not incrementing the DATABASE_VERSION.
Your options to try which will all cause the database to get recreated are:
Clear the applications data via the Settings apk
Manually uninstall then reinstall your application. (Just running gradle installDebug or having Eclipse install your app isn't good enough)
Increment your DATABASE_VERSION value
I would say that you need some space in Create statement.
Starting: COLUMN_QUESTION_TEXT + "text," into COLUMN_QUESTION_TEXT + " text,".
It looks just like you don't have spaces between the column names and the column types. Try this.
private static final String DATABASE_CREATE = "create table " + TABLE_JOKES
+ "( " + COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_CATEGORY + " text not null, " + COLUMN_SUBCATEGORY
+ " text not null, " + COLUMN_JOKE_TYPE + " integer not null, "
+ COLUMN_DESCRIPTION + " text," + COLUMN_QUESTION_TEXT + " text,"
+ COLUMN_ANSWER_TEXT + " text," + COLUMN_MONOLOGUE_TEXT + " text,"
+ COLUMN_RATING_SCALE + " integer," + COLUMN_COMMENTS + " text,"
+ COLUMN_JOKE_SOURCE + " text," + COLUMN_RELEASE_STATUS + " integer,"
+ COLUMN_CREATED + " text," + COLUMN_MODIFIED + " text" + ");";
In DATABASE_CREATE mistake "integer," + COLUMN_COMMENTS + "text,". No sepparate space.

Categories