0

我是使用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
    }

}

}

我已经尝试了大约一周来解决我的问题,比如更改数据库版本,更改第二个表的创建语句等,但它没有工作:(这个论坛中有很多问题与我的问题相同,但它不能解决我的问题,比如herehere等。

伙计们,请帮助我,。.

之前谢谢。

4

3 回答 3

0

看来您可能需要在“创建”字符串中添加一些空格。它也应该是exists而不是exist。尝试...

private static final String CREATE_TABLE_KRITERIA = "create table if not exists "
+TABLE_KRITERIA+ " ("+ KRITERIA_ID
+ " integer primary key autoincrement, " 
+KRITERIA_NAMA+ " text);";
于 2012-06-13T19:25:23.307 回答
0

如果您的代码仍然相同,那么您的第一个代码create table缺少空格。

你有

private static final String CREATE_TABLE_MASTER = "create table"

它应该是

private static final String CREATE_TABLE_MASTER = "create table "

另外,你能发布你的错误信息吗?

在 onUpgrade() 中,您应该执行升级。最快的方法是删除表然后重新创建。

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.i("MFrameworkAHP", "Upgrading database to " + newVersion);
    db.execSQL("drop table if exists " + TABLE_KRITERIA);
    db.execSQL("drop table if exists " + TABLE_MASTER);
    onCreate(db);
}

   public void onCreate(SQLiteDatabase db) {
        Log.i("MFrameworkAHP", CREATE_TABLE_KRITERIA);
        db.execSQL(CREATE_TABLE_KRITERIA);

        Log.i("MFrameworkAHP", CREATE_TABLE_MASTER);
        db.execSQL(CREATE_TABLE_MASTER);
    }

这将删除升级中不理想的所有内容,但至少它会在您更改数据库版本时重新创建测试中的表。

于 2012-06-13T19:44:37.200 回答
0

我刚才遇到了这个问题,解决方案是从一开始就创建所有表。如果您创建一个只有一个表用于测试的数据库,然后您尝试创建其余表,它将无法工作。因此,定义一个新的数据库名称并立即创建您需要的所有表,这就足够了。

于 2016-06-16T14:43:12.153 回答