我是使用sqlite数据库android的新手。现在我在 android 中的 sqlite 数据库有问题,首先我创建第一个表:
private static final String CREATE_TABLE_MASTER = "create table"
+ TABLE_MASTER + " (" + MASTER_ID
+ " integer primary key autoincrement, " + NAMA_MASALAH
+ " Integer not null, " + JLH_KRITERIA + " Integer not null, "
+ JLH_ALTERNATIVE + " text not null);";
这是成功的,但是当我创建第二个表时:
private static final String CREATE_TABLE_KRITERIA = "create table if not exist "
+TABLE_KRITERIA+ "("+ KRITERIA_ID
+ "integer primary key autoincrement, "
+KRITERIA_NAMA+ "text);";
但这是错误的,在 LogCat 中它说:
没有这样的表:tableKriteria:编译时:从 tableKriteria 中选择 idKriteria、namaKriteria
这里是我的 SQLiteAdapter.java 的完整代码:
package com.MFrameworkAHP;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class SQLiteAdapter {
public static final String NAMA_DATABASE = "MY_DATABASE";
public static final String TABLE_MASTER = "MY_TABLE";
public static final int MYDATABASE_VERSION = 1;
public static final String MASTER_ID = "_id";
public static final String NAMA_MASALAH = "Content1";
public static final String JLH_KRITERIA = "Content2";
public static final String JLH_ALTERNATIVE = "Content3";
public static final String TABLE_KRITERIA = "tableKriteria";
public static final String KRITERIA_ID= "idKriteria";
public static final String KRITERIA_NAMA = "namaKriteria";
public static final String KRITERIA_IDDATA = "idDataKriteria";
// create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String CREATE_TABLE_MASTER = "create table"
+ TABLE_MASTER + " (" + MASTER_ID
+ " integer primary key autoincrement, " + NAMA_MASALAH
+ " Integer not null, " + JLH_KRITERIA + " Integer not null, "
+ JLH_ALTERNATIVE + " text not null);";
private static final String CREATE_TABLE_KRITERIA = "create table if not exist "
+TABLE_KRITERIA+ "("+ KRITERIA_ID
+ "integer primary key autoincrement, "
+KRITERIA_NAMA+ "text);";
;
//private static final String
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public SQLiteAdapter(Context c) {
context = c;
}
public SQLiteAdapter openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, NAMA_DATABASE, null,
MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public SQLiteAdapter openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, NAMA_DATABASE, null,
MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}
public void close() {
sqLiteHelper.close();
}
public long insert(String namaMasalah, String jlhKriteria, String jlhAlternative) {
ContentValues contentValues = new ContentValues();
contentValues.put(NAMA_MASALAH, namaMasalah);
contentValues.put(JLH_KRITERIA, jlhKriteria);
contentValues.put(JLH_ALTERNATIVE, jlhAlternative);
return sqLiteDatabase.insert(TABLE_MASTER, null, contentValues);
}
public long insertKriteria(String namaKriteria){
ContentValues cv = new ContentValues();
//cv.put(KRITERIA_ID, idKriteria);
cv.put(KRITERIA_NAMA,namaKriteria);
return sqLiteDatabase.insert(TABLE_KRITERIA, null, cv);
}
public int deleteAll() {
return sqLiteDatabase.delete(TABLE_MASTER, null, null);
}
public void delete_byID(int id) {
sqLiteDatabase.delete(TABLE_MASTER, MASTER_ID + "=" + id, null);
}
public void update_byID(int id, String v1, String v2, String v3) {
ContentValues values = new ContentValues();
values.put(NAMA_MASALAH, v1);
values.put(JLH_KRITERIA, v2);
values.put(JLH_ALTERNATIVE, v3);
sqLiteDatabase
.update(TABLE_MASTER, values, MASTER_ID + "=" + id, null);
}
public Cursor queueAll() {
String[] columns = new String[] { MASTER_ID, NAMA_MASALAH, JLH_KRITERIA,
JLH_ALTERNATIVE };
Cursor cursor = sqLiteDatabase.query(TABLE_MASTER, columns, null,
null, null, null, null);
return cursor;
}
public Cursor queueKriteria() {
String[] columns = new String[] { KRITERIA_ID, KRITERIA_NAMA };
Cursor cursor = sqLiteDatabase.query(TABLE_KRITERIA, columns, null,
null, null, null, null);
return cursor;
}
public Cursor queueAll_SortBy_namaMasalah() {
String[] columns = new String[] { MASTER_ID, NAMA_MASALAH, JLH_KRITERIA,
JLH_ALTERNATIVE };
Cursor cursor = sqLiteDatabase.query(TABLE_MASTER, columns, null,
null, null, null, NAMA_MASALAH);
return cursor;
}
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_TABLE_KRITERIA);
db.execSQL(CREATE_TABLE_MASTER);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
}
我已经尝试了大约一周来解决我的问题,比如更改数据库版本,更改第二个表的创建语句等,但它没有工作:(这个论坛中有很多问题与我的问题相同,但它不能解决我的问题,比如here,here等。
伙计们,请帮助我,。.
之前谢谢。