I'm trying to follow this tutorial, but I'm getting this error message, when I run on my device:
08-04 01:40:12.820 27896-27896/com.filipeferminiano.quiz E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.filipeferminiano.quiz/com.filipeferminiano.quiz.MyActivity}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.filipeferminiano.quiz/databases/triviaQuiz
How can I solve this?
Try to replace this class,hope this will help you to solve your problem.
public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "triviaQuiz";
// tasks table name
private static final String TABLE_QUEST = "quest";
// tasks Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; //correct option
private static final String KEY_OPTA= "opta"; //option a
private static final String KEY_OPTB= "optb"; //option b
private static final String KEY_OPTC= "optc"; //option c
private SQLiteDatabase dbase;
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
+ " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, "
+KEY_OPTB +" TEXT, "+KEY_OPTC+" TEXT)";
db.execSQL(sql);
addQuestions(db);
}
private void addQuestions(SQLiteDatabase db)
{
Question q1=new Question("Which company is the largest manufacturer" +
" of network equipment?","HP", "IBM", "CISCO", "C");
this.addQuestion(q1,db);
Question q2=new Question("Which of the following is NOT " +
"an operating system?", "SuSe", "BIOS", "DOS", "B");
this.addQuestion(q2,db);
Question q3=new Question("Which of the following is the fastest" +
" writable memory?","RAM", "FLASH","Register","C");
this.addQuestion(q3,db);
Question q4=new Question("Which of the following device" +
" regulates internet traffic?", "Router", "Bridge", "Hub","A");
this.addQuestion(q4,db);
Question q5=new Question("Which of the following is NOT an" +
" interpreted language?","Ruby","Python","BASIC","C");
this.addQuestion(q5,db);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
// Create tables again
onCreate(db);
}
// Adding new question
public void addQuestion(Question quest,SQLiteDatabase db) {
ContentValues values = new ContentValues();
values.put(KEY_QUES, quest.getQUESTION());
values.put(KEY_ANSWER, quest.getANSWER());
values.put(KEY_OPTA, quest.getOPTA());
values.put(KEY_OPTB, quest.getOPTB());
values.put(KEY_OPTC, quest.getOPTC());
// Inserting Row
db.insert(TABLE_QUEST, null, values);
}
public List<Question> getAllQuestions() {
List<Question> quesList = new ArrayList<Question>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
dbase=this.getReadableDatabase();
Cursor cursor = dbase.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Question quest = new Question();
quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(1));
quest.setANSWER(cursor.getString(2));
quest.setOPTA(cursor.getString(3));
quest.setOPTB(cursor.getString(4));
quest.setOPTC(cursor.getString(5));
quesList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
dbase.close();
return quesList;
}
public int rowcount()
{
int row=0;
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
row=cursor.getCount();
return row;
}
}
the trace already told u the reason why it crashed:
attempt to re-open an already-closed object: SQLiteDatabase
check your database objects it closed some where in your code also check in tutorial there are not anywhere write db.close() so do not close your data base in your example..
thats it...
Related
Unfortunately all the solutions I could find, would not help me with my problem.
With a button click I want to add some value to my database table, but first it should check if the row already exists in my table, if so it just should update the row.
Here are my Codes:
MAIN ACTIVITY: (Just the Button Click)
ingredient= new Ingredient();
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i = 0; i < dataSource.size();i++){
tvname = (TextView) shoppingList.getChildAt(i).findViewById(R.id.tvName);
tvamount = (TextView) shoppingList.getChildAt(i).findViewById(R.id.tvAmount);
String nameTV = tvname.getText().toString();
String amountTV = tvamount.getText().toString();
ingredient.setName(nameTV);
ingredient.setAmount(amountTV);
ingredient.setId(i);
TableIngredient.getInstance(IngredientShopping.this).checkRow(ingredient);
}
DATABASE TABLE:
public class TableIncredient extends SQLiteOpenHelper {
public static TableIngredient INSTANCE = null;
public static final String DB_NAME = "INGREDIENT_TABLE";
public static final int VERSION = 1;
public static final String TABLE_NAME = "ingredient_table";
public static final String KEY_ID = "ID";
public static final String COL_NAME = "Name";
public static final String COL_AMOUNT = "AMOUNT";
public TableIngredient (Context context) {
super(context, DB_NAME, null, VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
String createQuery = "CREATE TABLE " + TABLE_NAME
+ "(" + KEY_ID + " INTEGER PRIMARY KEY, "
+ COL_NAME + " TEXT NOT NULL, "
+ COL_AMOUNT + " INTEGER DEFAULT NULL)";
db.execSQL(createQuery);
}
public boolean checkRow(Ingredient ingredient){
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.query(TABLE_NAME,
new String[]{KEY_ID, COL_NAME, COL_AMOUUNT},
KEY_ID + " =? AND " + COL_NAME + " =? AND " + COL_AMOUNT + " =?" ,
new String[]{String.valueOf(ingredient)},
null, null, null, null);
ContentValues values = new ContentValues();
values.put(COL_NAME, ingredient.getName());
values.put(COL_AMOUNT, ingredient.getAmount());
if (c.moveToFirst()){
increseIngredient(ingredient);
return true;
}
else {
long newID = db.insert(TABLE_NAME, null, values);
db.close();
getIngredient(newID);
return false;
}
}
}
With this code it always uses the else statement and I do not know why this happens.
I hope someone can help me to create a new row if it not exists and update the row if it exists.
With this code it always uses the else statement and I do not know why
this happens.
The reason why this happens is that the 4th parameter to the query method is new String[]{String.valueOf(ingredient)}
This will resolve to being a value something like Ingredient#1091 do you have an ingredient row that has an ID like that (rhetorical as that cannot be the case due to the ID column being an alias of the rowid and therefore ONLY integer values can be stored).
The reason that Ingredient#1091 is that the String value of the Ingredient object will be a pointer to the object.
As such no rows will ever be returned.
If instead you used new String[]{String.valueOf(ingredient.getId),ingredient.getName,ingredient.getAmount)}
The 3 values would (should), be the correct values (assuming that the ID is stored correctly in the Ingredient).
I recently launched an app on the android app store that contained a SQLite database.
I am now attempting to release an update of the app, and want to add more data into the existing database, however have come a bit unstuck. I have read answers on SO that outline making changes to the database itself, however I want my tables and columns to stay the same, only add new data in.
The data that i want to add to the database is pulled from CSV files in the Raw file, and originally loaded into the database when the user registers for the app.
I have a feeling I am going to need to implement the onUpgrade method, however should I be adding the new data from the CSV files in at that point as well? Is it a matter of simple updating the database version and using the onUpgrade to load the new data?
I am fairly new to SQLite DB, so any help would be hugely appreciated.
CourseDBHelper Code
public class CourseDBHelper extends SQLiteOpenHelper {
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "CourseDB";
// Create two table names
private static final String TABLE_COURSES = "courses";
// Universities Table Columns names
private static final String COURSE_NAME = "Course_name";
private static final String UNI_NAME = "Uni_name";
private static final String COURSE_DURATION = "Duration";
private static final String COURSE_STUDY_MODE = "Study_mode";
private static final String COURSE_QUALIFICATION = "Qualification";
private static final String COURSE_ENTRY_STANDARDS = "Entry_standards";
private static final String COURSE_GRADUATE_PROSPECTS = "Graduate_prospects";
private static final String COURSE_STUDENT_SATISFACTION = "Student_satisfaction";
private String CREATE_COURSES_TABLE = "CREATE TABLE courses" +
"(" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"Course_name TEXT NOT NULL," +
"Uni_name TEXT NOT NULL," +
"Duration TEXT NOT NULL," +
"Study_mode TEXT NOT NULL," +
"Qualification TEXT NOT NULL," +
"Entry_standards TEXT NOT NULL," +
"Graduate_prospects TEXT NOT NULL," +
"Student_satisfaction TEXT NOT NULL" +
");";
private static final String[] COLUMNS = {
COURSE_NAME,
UNI_NAME,
COURSE_DURATION,
COURSE_STUDY_MODE,
COURSE_QUALIFICATION,
COURSE_ENTRY_STANDARDS,
COURSE_GRADUATE_PROSPECTS,
COURSE_STUDENT_SATISFACTION
};
public CourseDBHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// TODO: REMOVED NOT NULL FROM EVERY COLUMN FOR TEST PURPOSES, WILL NEED TO BE READDED
#Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(CREATE_COURSES_TABLE);
}
public void deleteAll()
{
SQLiteDatabase db = this.getWritableDatabase();
db.delete("courses", null, null);
db.execSQL("delete from " + "courses");
db.close();
}
// Getting one course by course name and uni name
public Course getCourse(String courseName, String uniName) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_COURSES, COLUMNS, " Course_name = ? AND Uni_name = ?",
new String[]{courseName, uniName},
null,
null,
null,
null);
if (cursor != null)
cursor.moveToFirst();
Course course = new Course();
/*
System.out.println(cursor.getString(0));
System.out.println(cursor.getString(1));
System.out.println(cursor.getString(2));
System.out.println(cursor.getString(3));
System.out.println(cursor.getString(4));
System.out.println(cursor.getString(5));
System.out.println(cursor.getString(6));
*/
course.setCourseName(cursor.getString(0));
course.setUniversity(cursor.getString(1));
course.setCourseDuration(cursor.getString(2));
course.setStudyMode(cursor.getString(3));
course.setQualification(cursor.getString(4));
course.setEntryStandards(cursor.getString(5));
course.setGradProspects(cursor.getString(6));
course.setStudentSatisfaction(cursor.getString(7));
return course;
}
public void addCourse(Course course)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COURSE_NAME, course.getCourseName());
values.put(UNI_NAME, course.getUniversity());
values.put(COURSE_DURATION, course.getCourseDuration());
values.put(COURSE_STUDY_MODE, course.getStudyMode());
values.put(COURSE_QUALIFICATION, course.getQualification());
values.put(COURSE_ENTRY_STANDARDS, course.getEntryStandards());
values.put(COURSE_GRADUATE_PROSPECTS, course.getGradProspects());
values.put(COURSE_STUDENT_SATISFACTION, course.getStudentSatisfaction());
db.insert(TABLE_COURSES,
null, //nullColumnHack
values);
db.close();
}
public ArrayList<Course> getAllCourses()
{
ArrayList<Course> courses = new ArrayList<>();
// 1. build the query
String query = "SELECT * FROM " + TABLE_COURSES;
// 2. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
// 3. go over each row, build course and add it to list
Course course;
if(cursor.moveToFirst()){
cursor.moveToNext();
do{
course = new Course();
course.setCourseName(cursor.getString(1));
course.setUniversity(cursor.getString(2));
course.setCourseDuration(cursor.getString(3));
course.setStudyMode(cursor.getString(4));
course.setQualification(cursor.getString(5));
course.setEntryStandards(cursor.getString(6));
course.setGradProspects(cursor.getString(7));
course.setStudentSatisfaction(cursor.getString(8));
// Add course to courses list
courses.add(course);
} while(cursor.moveToNext());
}
// return courses
return courses;
}
public int getDBCount()
{
SQLiteDatabase db = this.getWritableDatabase();
String count = "SELECT count(*) FROM courses";
Cursor mcursor = db.rawQuery(count, null);
mcursor.moveToFirst();
int icount = mcursor.getInt(0);
return icount;
}
public void deleteCourse(Course course) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. delete
db.delete("courses", //table name
"Course_name = ? AND Uni_name = ?", // selections
new String[] { course.getCourseName(), course.getUniversity() }); //selections args
// 3. close
db.close();
}
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
Method that loads data from CSV file to SQlite DB
public void populateCourseDatabase(int id) {
// NOW POPULATE THE COURSE DATABASE FILE
inputStream = getResources().openRawResource(R.raw.coursesone);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String word;
String cvsSplitBy = ",";
try{
while((word = reader.readLine()) != null){
Log.d(TAG, "constructing Course object from: " + word);
String[] segment = word.split(cvsSplitBy);
Course course = new Course();
course.setCourseName(segment[0]);
course.setUniversity(segment[1]);
course.setCourseDuration(segment[2]);
course.setStudyMode(segment[3]);
course.setQualification(segment[4]);
course.setEntryStandards(segment[5]);
course.setGradProspects(segment[6]);
course.setStudentSatisfaction(segment[7]);
myCourseDBHelper.addCourse(course);
progressBar.setProgress(count);
count = count + 1;
System.out.println("Sucessfully added: " + course.toString());
}
}
catch(IOException e1){
e1.printStackTrace();
System.out.println("SOMETHING WENT WRONG");
}
}
SQLiteOpenHelper onCreate() and onUpgrade() callbacks are invoked when the database is actually opened, for example by a call to getWritableDatabase().onCreate() is only run when the database file did not exist and was just created. onUpgrade() is only called when the database file exists but the stored version number is lower than requested in constructor.Increment the database version so that onUpgrade() is invoked.
Example pseudo code below
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
switch(oldVersion) {
case 1:
//upgrade logic from version 1 to 2
case 2:
//upgrade logic from version 2 to 3
case 3:
//upgrade logic from version 3 to 4
break;
default:
throw new IllegalStateException(
"onUpgrade() with unknown oldVersion " + oldVersion);
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have been through a few sqlite tutorials and wrote this code on my own to reinforce the principles. The tutorials I went through varied widely in a few areas so this is what I came up with as a combination of everything.
Other than the two classes I have listed below, All I have is one activity that adds, remove, inserts, update and display data from the database.
I am seeking any feedback whatsoever. However I have a few specific questions. Thanks in advance.
When should I close the helper class or does garbage collection deal with it automatically?
Should I create a Boxer POJO(Plain old java object to pass boxer data to and from the DAO?
Was the DAO implementation Efficient?
Does the code deviate from Java and Android best practices in anyway?
Any constructive advice is welcome
Helper Class
public class BoxScoresHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "boxing_scores.db";
private static final int VERSION = 1;
private static BoxScoresHelper instance = null;
public static BoxScoresHelper getInstance(Context context){
if(instance == null){
instance = new BoxScoresHelper(context);
}
return instance;
}
private BoxScoresHelper(Context context) {
super(context, DB_NAME, null, VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(createBoxerSQLString());
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("Drop Table If Exists " + BoxerDAO.TABLE_NAME);
onCreate(db);
}
private String createBoxerSQLString(){
String boxerCreateString = "create table " + BoxerDAO.TABLE_NAME +
"(" + BoxerDAO._ID + " Integer Primary Key AutoIncrement, " +
BoxerDAO.BOXER_NAME + " Text Not Null, " +
BoxerDAO.WEIGHT_CLASS + " Text Not Null, " +
BoxerDAO.WINS + " Integer Not Null, " +
BoxerDAO.LOSSES + " Integer Not Null);";
return boxerCreateString;
}
}
DAO Class
public class BoxerDAO {
public static final String TABLE_NAME = "Boxer";
public static final String _ID = "_id";
public static final String BOXER_NAME = "boxer_name";
public static final String WEIGHT_CLASS = "weight_class";
public static final String WINS = "wins";
public static final String LOSSES ="losses";
private final BoxScoresHelper myScoresHelper;
private SQLiteDatabase myBoxerDB;
public BoxerDAO(Context context){
myScoresHelper = BoxScoresHelper.getInstance(context);
}
public Cursor query(String[] projection,String selection,String[] selectionArgs, String orderBy){
Cursor cursor;
myBoxerDB = myScoresHelper.getReadableDatabase();
cursor = myBoxerDB.query(TABLE_NAME, projection, selection, selectionArgs, null, null, orderBy);
//myBoxerDB.close();
return cursor;
}
public Cursor queryAll(){
Cursor cursor;
myBoxerDB = myScoresHelper.getReadableDatabase();
cursor = myBoxerDB.rawQuery("Select * From " + TABLE_NAME, null);
//myBoxerDB.close();
return cursor;
}
public int delete(int id){
int rowsDel;
myBoxerDB = myScoresHelper.getWritableDatabase();
rowsDel = myBoxerDB.delete(TABLE_NAME, _ID + " = " + id , null);
//myBoxerDB.close();
return rowsDel;
}
public long insert(ContentValues values){
long insertId = -1;
myBoxerDB = myScoresHelper.getWritableDatabase();
insertId = myBoxerDB.insert(TABLE_NAME, null, values);
//myBoxerDB.close();
return insertId;
}
public int update(ContentValues values,String selection, String[] selectionArgs){
int updatedRows;
myBoxerDB = myScoresHelper.getWritableDatabase();
updatedRows = myBoxerDB.update(TABLE_NAME, values, _ID + " = " + selection, selectionArgs);
//myBoxerDB.close();
return updatedRows;
}
}
When I run my android app I keep getting the error (1) no such table userTable, as far as I am aware I am creating the table in
TABLE_CREATE = "create table userTable("
+ USER + " text not null, " + PWD + " text not null, );";
I have two other databases in this project and the are working fine, any help is welcome.
package com.weightpro.db;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class usrPwdDB {
public static final String USER = "userName";
public static final String PWD = "password";
public static final String TABLE_NAME = "userTable";
public static final String DATA_BASE_NAME = "userdatabase";
public static final String KEY_ROWID = "_id";
public static final int DB_VERSION = 2;
private static final String TABLE_CREATE = "create table userTable("
+ USER + " text not null, " + PWD + " text not null, );";
DBHelper WDBHelper;
Context mContext;
SQLiteDatabase db;
public usrPwdDB(Context mContext) {
this.mContext = mContext;
WDBHelper = new DBHelper(mContext);
}
private static class DBHelper extends SQLiteOpenHelper
{
public DBHelper(Context context) {
super(context,DATA_BASE_NAME, null, DB_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
try{
db.execSQL(TABLE_CREATE);
}
catch(SQLException e)
{
e.printStackTrace();
}
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS userTable");
onCreate(db);
}
}
public usrPwdDB open()
{
db = WDBHelper.getWritableDatabase();
return this;
}
public void close(){
WDBHelper.close();
}
public long insertInfo(String userName, String password){
ContentValues content = new ContentValues();
content.put(USER, userName);
content.put(PWD, password);
return db.insertOrThrow(TABLE_NAME, null, content);
}
public boolean getUserNameAndPassword(String userName, String Password) throws SQLException {
Cursor mCursor =
db.query(true, TABLE_NAME, new String[] {USER,
PWD},USER+"='"+userName+"' AND password='"+Password+"'", null,
null, null, null, null);
if (mCursor.getCount() > 0)
{
return true;
}
return false;}
public Cursor returnData(){
return db.query(TABLE_NAME, new String[] {USER, PWD},null,null,null,null,null);
}
}
remove the last comma , from the create table syntax,
private static final String TABLE_CREATE =
"create table userTable("
+ USER + " text not null, "
+ PWD + " text not null, );"; // remove this comma after not null
The correct syntax should be
private static final String TABLE_CREATE =
"create table userTable("
+ USER + " text not null, "
+ PWD + " text not null );";
You go wrong over here
+ USER + " text not null, " + PWD + " text not null, );"; //remove , from last text not null
correct SQL command with below
private static final String TABLE_CREATE = "create table userTable("
+ USER + " text not null, " + PWD + " text not null);" ;
private static final String TABLE_CREATE = "create table userTable("
+ USER + " text not null, " + PWD + " text not null,);" ;
remove "," after "text not null" and also remove ";"(semicolon) in the string, that is not needed i think,
then clear data or uninstall application then execute again
even though the creation of table failed i think next time onCreate() in SQLiteOpenHelper only executes once, so you will need to clear data of application
if you are using separate class for separate table and using same DB name, then all the tables in the first using class will be ok, and others will not be created since for a single database onCreate() function works only one, even if there is separate classes each with onCreate(),
onCreate() function creates a file in DB location of android file system when first called onCreate(), if the onCreate called again it check for the existence of DBNAME file in DB location and avoid creating if its exit, I think this is logic behind it...
so keep one onCreate() function for a single DB file and create all tables in that function
This is my Database class
public class ProgramDbAdapter{
public static final String KEY_ROWID = "_id";
public static final String KEY_PROGRAM_TITLE = "ProgramTitle";
public static final String KEY_PROGRAM_DATE = "ProgramDate";
public static final String KEY_PROGRAM_TIME = "ProgramTime";
public static final String KEY_PROGRAM_CHANNEL = "ProgramChannel";
private static final String DATABASE_CREATE =
"CREATE TABLE " + DATABASE_TABLE + " (" + "_id integer primary key autoincrement, "
+ "ProgramTitle text, " + "ProgramDate varchar(20), "
+ "ProgramTime varchar(20), " + "ProgramChannel text)";
private static final String DATABASE_UPGRADE = "DROP TABLE IF EXISTS " + DATABASE_TABLE;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db){
db.execSQL(DATABASE_CREATE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL(DATABASE_UPGRADE);
onCreate(db);
}
}
public ProgramDbAdapter open() throws SQLException{
mDbHelper = new DatabaseHelper(mcontext);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public long CreateData(String ProgramTitle, String ProgramDate, String ProgramTime, String ProgramChannel){
ContentValues initialvalues = new ContentValues();
initialvalues.put(KEY_PROGRAM_TITLE, ProgramTitle);
initialvalues.put(KEY_PROGRAM_DATE, ProgramDate);
initialvalues.put(KEY_PROGRAM_TIME, ProgramTime);
initialvalues.put(KEY_PROGRAM_CHANNEL, ProgramChannel);
return mDb.insert(DATABASE_TABLE, null, initialvalues);
}
}
This is my main class
public class Program extends ExpandableListActivity{
public void onCreate(Bundle savedInstanceState) {
mDbHelper = new ProgramDbAdapter(this);
mDbHelper.open();
mDbHelper.CreateData("a","a","a","a");
}
}
when i call open() and createdata function, the logcat tell me no such table:Program. where is the problem is?
There is no varchar type in sqlite, see here. Use text instead.
I don't see where 'DATABASE_TABLE' is coming from.
Also, you should use the "rawQuery()", "query()"-Methods or a "SQLiteStatement" to bind parameters into your SQL-Statement.
And last but not least, SQLite doesn't know any 'varchar'. Check here for all Data types: Link
varchar is translated to text ...
from vladimir link:
If the declared type of the column
contains any of the strings "CHAR",
"CLOB", or "TEXT" then that column has
TEXT affinity. Notice that the type
VARCHAR contains the string "CHAR" and
is thus assigned TEXT affinity.
problem is that u created db in first run and there was different Create statment ...
delete app data from android settings or change DATABASE_VERSION