"no such colum : time (Code 1)" SQLiteException in android studio - android

onCreate method of the database :
private static String DATABASE = "DB1.db";
private static String TABLE ="NotesTable";
private static String TITLE = "title";
private static String NAME ="name";
private static String TIME = "time";
private static String BOOKMARK = "bookm";
private static String NOTITIME = "notitime";
DatabaseHelper(Context context) {
super(context, DATABASE, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "
+ TABLE + "( "
+ TITLE + " TEXT, "
+ NAME + " TEXT, "
+ NOTITIME + " LONG, "
+ BOOKMARK + " INTEGER DEFAULT 1 )");
}
Error occurs in the following code of the database :
//cursor to get all the column's data
List<DataModel> getdata(){
List<DataModel> data=new ArrayList<>();
SQLiteDatabase db = this.getWritableDatabase();
StringBuilder stringBuffer = new StringBuilder();
DataModel dataModel;
#SuppressLint("Recycle") Cursor cursor = db.rawQuery("select * from "+TABLE+" ORDER BY " + TIME + " DESC;",null);
while (cursor.moveToNext()) {
dataModel= new DataModel();
String title = cursor.getString(cursor.getColumnIndexOrThrow("title"));
String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
Long time = cursor.getLong(cursor.getColumnIndexOrThrow("time"));
Integer book = cursor.getInt(cursor.getColumnIndexOrThrow("bookm"));
Long notitime = cursor.getLong(cursor.getColumnIndexOrThrow("notitime"));
dataModel.setTitle(title);
dataModel.setName(name);
dataModel.setDate(time);
dataModel.setBookmark(book);
dataModel.setNotiTime(notitime);
stringBuffer.append(dataModel);
data.add(dataModel);
}
return data;
}
StackTrace :
FATAL EXCEPTION: main
Process: com.tank.vishal.sample, PID: 5981
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tank.vishal.sample/com.tank.vishal.sample.MainActivity}: android.database.sqlite.SQLiteException: no such column: time (code 1): , while compiling: select * from NotesTable ORDER BY time DESC;
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
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 column: time (code 1): , while compiling: select * from NotesTable ORDER BY time DESC;
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 com.tank.vishal.sample.DatabaseHelper.getdata(DatabaseHelper.java:102)
at com.tank.vishal.sample.MainActivity.initViews(MainActivity.java:114)
at com.tank.vishal.sample.MainActivity.onCreate(MainActivity.java:83)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
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) 
What I've tried :
Changing the name of table and database,
Uninstalling the app itself and remove all the data from device.
this error never occurred before while making this app, it was running perfectly till today.
P.S. - I tried installing the app in a device in which it is not installed before NOT EVEN ONCE and it returned below error :
Failure [INSTALL_CANCELED_BY_USER]
$ adb shell pm uninstall com.tank.vishal.sample
DELETE_FAILED_INTERNAL_ERROR
Error while Installing APK

Your CREATE TABLE SQL does not create a column with the name of time. First add the column specification for it and only then uninstall and reinstall your app.

In onCreate, you didn't create column time

Related

SQLite Column Error : table XXXX has no column named ZZZZ

I'm trying to insert rows in my Student Table which contains two rows : ID and Name
Here is the addHandler function which is implemented in MyDBHandler class :
public void addHandler(Student student) {
ContentValues values = new ContentValues();
values.put(COLUMN_ID, student.getID());
values.put(COLUMN_NAME, student.getStudentName());
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_NAME, null, values);
db.close();
}
The onCreate method is :-
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY ," + COLUMN_NAME + " TEXT )";
db.execSQL(CREATE_TABLE);
}
The attributes of MyDBHandler class which extends SQLiteOpenHelper :
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "studentDB.db";
public static final String TABLE_NAME = "Student";
public static final String COLUMN_ID = "StudentID";
public static final String COLUMN_NAME = "StudentName";
I have a ADD Button in my activity_main.xml file and here is the code behind :
public void add(View view){
MyDBHandler dbHandler = new MyDBHandler(this, null, null, 1);
int id = Integer.parseInt(studentIdText.getText().toString());
String name = studentNameText.getText().toString();
Student student = new Student(id, name);
dbHandler.addHandler(student);
studentIdText.setText("");
studentNameText.setText("");
}
The app is running perfectly but when i want to insert a row in the table , i get the following errors in Run Tab :
E/SQLiteLog: (1) table Student has no column named StudentID
E/SQLiteDatabase: Error inserting StudentName=yassine StudentID=10
android.database.sqlite.SQLiteException: table Student has no column named StudentID (code 1): , while compiling: INSERT INTO Student(StudentName,StudentID) VALUES (?,?)
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(table Student has no column named StudentID (code 1): , while compiling: INSERT INTO Student(StudentName,StudentID) VALUES (?,?))
#################################################################
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.insertWithOnConflict(SQLiteDatabase.java:1607)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1479)
at com.example.test.MyDBHandler.addHandler(MyDBHandler.java:48)
at com.example.test.MainActivity.add(MainActivity.java:40)
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:385)
at android.view.View.performClick(View.java:5246)
at android.widget.TextView.performClick(TextView.java:10566)
at android.view.View$PerformClick.run(View.java:21256)
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:6917)
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:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Any recommendations ?
Your issue is very likely a mis-conception in regard to the onCreate method. That is it onCreate doesn't run every time the App is run. onCreate will only run if the database doesn't actually exist. If the database has already been created by another previous run of the App then the onCreate method is not run.
As such any changes made to the structure of the database, as typically applied in the onCreate method will not be applied.
It would appear that you have added the defnition for the StudentId column, to the code in the onCreate method, after the App has been run.
As long as you have no data that needs to be preserved (which is very likely) then the simplest solution is to do 1 of the following :-
delete or clear the App's data (via settings/Apps)
uninstall the App
and then rerun the App, the database will then be created using the code as per the modified onCreate code.

SQLiteException: no such column: DeletedNotes (code 1) [duplicate]

This question already has answers here:
When does SQLiteOpenHelper onCreate() / onUpgrade() run?
(15 answers)
Closed 5 years ago.
I have a Table named as notes and a method called getlist() which retrieve data from the database. Now the problem is when getlist() method is executed the application crashes and it says that no such column DeletedNotes even tho it is specified in create table statement.
Here is my code:
public class DBOpenHelper extends SQLiteOpenHelper {
public static final String DB_Name="Notes.db";
public static final int DB_Version=1;
public static final String Table_Name="NoteData";
public static final String id="_id";
public static final String Note_Title="noteTitle";
public static final String Note_Text="NoteText";
public static final String Note_Created="NoteCreated";
public static final String DELETED_NOtes="DeletedNotes";
public static final String Archived_Notes="ArchiveNotes";
public static final int True=1;
public static final int False=0;
//SQLite Query
public static final String Table_Create=
"CREATE TABLE "+Table_Name+"("
+id+" INTEGER PRIMARY KEY, "
+Note_Title+" TEXT, "
+Note_Text+" TEXT, "
+Note_Created+" TEXT default CURRENT_TIMESTAMP, "
+DELETED_NOtes+"INTEGER DEFAULT "+False+", "
+Archived_Notes+"INTEGER DEFAULT "+False+" "
+");";
public DBOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DB_Name, null, DB_Version);
}
#Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(Table_Create);
}
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+Table_Name);
onCreate(sqLiteDatabase);
}
public Cursor getList(){
SQLiteDatabase sqLiteDatabase=this.getWritableDatabase();
Cursor data=sqLiteDatabase.rawQuery("Select * from "+Table_Name+" WHERE "+DELETED_NOtes+" = "+False+" AND "+Archived_Notes+" = "+False+"; ",null);
return data;
}
and here is my logcat:
08-26 18:52:14.812 8731-8731/com.example.chirag.stickynotes E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chirag.stickynotes, PID: 8731
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chirag.stickynotes/com.example.chirag.stickynotes.MainActivity}: android.database.sqlite.SQLiteException: no such column: DeletedNotes (code 1): , while compiling: Select * from NoteData WHERE DeletedNotes = 0 AND ArchiveNotes = 0;
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.database.sqlite.SQLiteException: no such column: DeletedNotes (code 1): , while compiling: Select * from NoteData WHERE DeletedNotes = 0 AND ArchiveNotes = 0;
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 com.example.chirag.stickynotes.DBOpenHelper.getList(DBOpenHelper.java:85)
at com.example.chirag.stickynotes.TextFragment.onCreateView(TextFragment.java:64)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2239)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1332)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1574)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1641)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:794)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2415)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2200)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2153)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2063)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:554)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
at android.app.Activity.performStart(Activity.java:6696)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
08-26 18:52:14.893 1585-1662/system_process W/ActivityManager: Force finishing activity com.example.chirag.stickynotes/.MainActivity
08-26 18:52:15.121 1585-3167/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
08-26 18:52:15.121 1585-3167/system_process D/OpenGLRenderer: Swap behavior 1
You should leave space beside the column name, DELETED_NOtes and Archived_Notes. In your code, since you don't have space between the column name and the type INTEGER, the table would have got created with a column name like this: DeletedNotesINTEGER
public static final String Table_Create =
"CREATE TABLE "+Table_Name +"("
+id+" INTEGER PRIMARY KEY, "
+Note_Title+" TEXT, "
+Note_Text+" TEXT, "
+Note_Created+" TEXT default CURRENT_TIMESTAMP, "
+DELETED_NOtes+" INTEGER DEFAULT "+False+", "
+Archived_Notes+" INTEGER DEFAULT "+False+" "
+");";

android.database.sqlite.SQLiteException: no such column: UPDATE

There is something wrong with my update statement. I am trying to update an user from my database, I am sure this user is in my database, but I just can't update him. I think I have a fault with my column name because the error said:
android.database.sqlite.SQLiteException: no such column: UPDATE
But I never asked for this column, nnn isn't a column at all in my scheme. Here is the statement
public void changeprofiel(String id, Profiel p){
SQLiteDatabase db = this.getWritableDatabase();
/* ContentValues contentValues = new ContentValues();
contentValues.put(COL_2_PROFIELEN, p.getUsername());
contentValues.put(COL_3_PROFIELEN, p.getFirstname());
contentValues.put(COL_4_PROFIELEN, p.getEmail());
contentValues.put(COL_5_PROFIELEN, p.getPassword());*/
// de insert methode geeft -1 terug als het niet gelukt is en de row value als het wel gelukt is
System.out.println(getProfielWid(id).getId());
// db.update(TABLE_NAME_PROFIELEN, contentValues,COL_1_PROFIELEN ="'" + id +"'",null);
db.execSQL("UPDATE "+ TABLE_NAME_PROFIELEN + " SET "+COL_2_PROFIELEN+" = "+p.getUsername()+", " +COL_3_PROFIELEN+" = "+p.getFirstname()+", "+COL_4_PROFIELEN+" = "+p.getEmail()+", "+COL_5_PROFIELEN+" = "+p.getPassword()+
" WHERE " + COL_1_PROFIELEN + "=" + id + "");
}
I also tried this way for the where closule:
" WHERE " + COL_1_PROFIELEN + "='" + id + "'");
But they give me both this error:
I/System.out: nnnnnn
E/SQLiteLog: (1) no such column: nnn
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cedri.bcv, PID: 13908
android.database.sqlite.SQLiteException: no such column: nnn (code 1): , while compiling: UPDATE profielen SET username = nnn, firstname = nnn, email = nnn, password = xxx WHERE profile_id=nnnnnn
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:1677)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
at com.example.cedri.bcv.DB.DatabaseHelper.changeprofiel(DatabaseHelper.java:82)
at com.example.cedri.bcv.Activities.MainActivity.savechanges(MainActivity.java:94)
at com.example.cedri.bcv.Fragments.AccountFragment$1.onClick(AccountFragment.java:86)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Do I need to set every value between single quotes?
You need to wrap your values with single quotes ('') if the column's type is text.
Your query should look like this:
UPDATE profielen SET username = 'nnn', firstname = 'nnn', email = 'nnn', password = 'xxx'
WHERE profile_id='nnnnnn'

cant Insert data into SQLite-android

I want to add data into my created SQLite database with SQLite database manager programs i get the "readonly database"error
When i comment insertion line the program works fine and crashes when uncommented.
Note : I have requested read/write storage permission and it has been granted.
public static Context context;
public static SQLiteDatabase database;
public static String SDK = Environment.getExternalStorageDirectory().toString();
public static String DB = SDK + "/database/";
context = getApplicationContext();
File file = new File(DB);
file.mkdirs();
database = SQLiteDatabase.openOrCreateDatabase(DB + "/db.sqlite",null);
database = SQLiteDatabase.openDatabase(DB + "/db.sqlite",null,SQLiteDatabase.OPEN_READWRITE);
database.execSQL("CREATE TABLE IF NOT EXISTS 'tbltest' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE , 'name' VARCHAR, 'family' VARCHAR)");
database.execSQL("INSERT INTO `tbltest` (`name`,`family`) VALUES (`John`,`Doe`)");
database.close();
Logcat error
` --------- beginning of crash
04-19 20:54:50.832 13353-13353/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: andishmand.database, PID: 13353
java.lang.RuntimeException: Unable to start activity ComponentInfo{andishmand.database/andishmand.database.MainActivity}: android.database.sqlite.SQLiteException: no such column: John (code 1): , while compiling: INSERT INTO `tbltest` (`name`,`family`) VALUES (`John`,`Doe`)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.database.sqlite.SQLiteException: no such column: John (code 1): , while compiling: INSERT INTO `tbltest` (`name`,`family`) VALUES (`John`,`Doe`)
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:1677)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
at andishmand.database.MainActivity.executecommand(MainActivity.java:128)
at andishmand.database.MainActivity.createdb(MainActivity.java:115)
at andishmand.database.MainActivity.requestperms(MainActivity.java:100)
at andishmand.database.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
04-19 20:54:50.853 1610-3265/? W/ActivityManager: Force finishing activity andishmand.database/.MainActivity`
Confirm that the device database is the way you want it as in your create statement in case your table was previously created without a column. You can do this by uninstalling the app and redeploying it.
Then use ContentValues class and the database.insert to do the actual insert. A good deal of this is explained here : https://developer.android.com/training/basics/data-storage/databases.html, but here is the short and simple:
remove this line :
database.execSQL("INSERT INTO `tbltest` (`name`,`family`) VALUES (`John`,`Doe`)");
And replace it with :
ContentValues values = new ContentValues();
values.put("name", "John");
values.put("family", "Doe");
long insertedRowId = database.insert("tbltest", null, values);
SQL uses "double quotes" to quote table/column names, and 'single quotes' to quote strings.
`Backticks` are a misguided attempt to be MySQL compatible; don't use them, ever.

Android Studio : E/SQLiteLog: (1) table places has no column named description_place

I keep getting this error "table places has no clumn named description_place" still pretty new to android so no clue why this happens, can anyone spot the error?
This is my Database class:
public class BDManager {
private static final String TABLE_NAME = "places";
public static final String KEY_ID_PLACE = "_id";
public static final String KEY_NOM_PLACE = "nom_place";
public static final String KEY_TYPE_PLACE = "type_place";
public static final String KEY_ADDRESS_PLACE = "address_place";
public static final String KEY_DESCRIPTION_PLACE = "description_place";
public static final String CREATE_TABLE_PLACES = "CREATE TABLE "
+ TABLE_NAME + "("
+ KEY_ID_PLACE + " INTEGER PRIMARY KEY, "
+ KEY_NOM_PLACE +" TEXT, "
+ KEY_TYPE_PLACE +" TEXT, "
+ KEY_ADDRESS_PLACE +" TEXT, "
+ KEY_DESCRIPTION_PLACE +" TEXT);";
private MySQLite maBaseSQLite;
private SQLiteDatabase db;
public BDManager(Context context){
maBaseSQLite = MySQLite.getInstance(context);
}
public void open() {
db = maBaseSQLite.getWritableDatabase();
}
public void close() {
db.close();
}
public long addPlace(BD place){
ContentValues values = new ContentValues();
values.put(KEY_NOM_PLACE, place.getNom_place());
values.put(KEY_TYPE_PLACE, place.getType_place());
values.put(KEY_ADDRESS_PLACE, place.getAddress_place());
values.put(KEY_DESCRIPTION_PLACE, place.getDescription_place());
return db.insert(TABLE_NAME,null,values);
}
public int modPlace(BD place){
ContentValues values = new ContentValues();
values.put(KEY_NOM_PLACE, place.getNom_place());
values.put(KEY_TYPE_PLACE, place.getType_place());
values.put(KEY_ADDRESS_PLACE, place.getAddress_place());
values.put(KEY_DESCRIPTION_PLACE, place.getDescription_place());
String where = KEY_ID_PLACE+" = ?";
String[] whereArgs = {place.getId_place()+""};
return db.update(TABLE_NAME, values, where, whereArgs);
}
public BD getPlace(int id){
BD p = new BD("","","","");
Cursor c = db.rawQuery("SELECT * FROM "+TABLE_NAME+" WHERE "+KEY_ID_PLACE+"="+id, null);
if (c.moveToFirst()){
p.setId_place(c.getInt(c.getColumnIndex(KEY_ID_PLACE)));
p.setNom_place(c.getString(c.getColumnIndex(KEY_NOM_PLACE)));
p.setType_place(c.getString(c.getColumnIndex(KEY_TYPE_PLACE)));
p.setAddress_place(c.getString(c.getColumnIndex(KEY_ADDRESS_PLACE)));
p.setDescription_place(c.getString(c.getColumnIndex(KEY_DESCRIPTION_PLACE)));
c.close();
}
return p;
}
public Cursor getPlaces(){
return db.rawQuery("SELECT * FROM "+TABLE_NAME, null);
}
}
It makes the error when I try tu save a place with the addplace function here :
public void savePlace(View view) {
final EditText name = (EditText) findViewById(R.id.NomPlace);
final Spinner type = (Spinner) findViewById(R.id.TypePlace);
final EditText address = (EditText) findViewById(R.id.AdressePlace);
final EditText description = (EditText) findViewById(R.id.DescriptionPlace);
BDManager sav = new BDManager(this);
sav.open();
sav.addPlace(new BD(name.getText().toString() ,type.getSelectedItem().toString() , address.getText().toString(),description.getText().toString()));
sav.close();
name.setText("");
type.setSelection(0);
address.setText("");
description.setText("");
}
Here is the complete error :
table places has no column named description_place
Error inserting description_place= nom_place=fyeyryfu address_place=dure type_place=Default
android.database.sqlite.SQLiteException: table places has no column named description_place (code 1): , while compiling: INSERT INTO places(description_place,nom_place,address_place,type_place) VALUES (?,?,?,?)
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(table places has no column named description_place (code 1): , while compiling: INSERT INTO places(description_place,nom_place,address_place,type_place) VALUES (?,?,?,?))
#################################################################
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.insertWithOnConflict(SQLiteDatabase.java:1607)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1479)
at com.myfavoriteplaces.myfavoriteplaces.BDManager.addPlace(BDManager.java:49)
at com.myfavoriteplaces.myfavoriteplaces.SavePlaces.savePlace(SavePlaces.java:124)
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:284)
at android.view.View.performClick(View.java:5076)
at android.view.View$PerformClick.run(View.java:20279)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
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:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Can someone help me please ?
Dude i was gonna ask for a magic wand but then.... eureka (no nudity like the greek dude is involved :-) ) I once had similar problem
A SINGLE SPACE BETWEEN THE WORD "TEXT AND THE BRACE IS NEEDED
KEY_DESCRIPTION_PLACE +" TEXT );";
I trust AN UPVOTE IS THE LEAST U CAN OFFER
mostly when a missing column error is crushing android success dream... its the cause of typing error.
Heil Android!!!
when you change database information you need to Drop table in OnOpdate()
if you want shere your onCreate and OnUpdate (MySQLite) code and i fix it

Categories

Resources