1

创建多个 SQLite 表时我遇到了奇怪的崩溃...

如果我注释掉执行 CREATE_MOVEMENTS_TABLE 的行,则代码可以正常工作。

第一个表的创建没有问题,如果我尝试创建两个表,我的应用程序崩溃。

奇怪的是:如果我在我的手机上测试应用程序而不创建第二个表,它工作正常,然后......如果我再次测试(安装了应用程序),但是使用两个 sql 语句,(创建两个表) ,应用程序工作......当手机中根本不存在应用程序时会出现问题

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_NAME = "ClientsManager";

    private static final String CLIENTS_TABLE = "Clients";
    private static final String MOVEMENTS_TABLE = "Movements";

    //Client Table Columns
    private static final String KEY_ROWID = "numint";
    private static final String KEY_NAME = "name";
    private static final String KEY_LOCAL = "locality";
    private static final String KEY_ADDR = "address";
    private static final String KEY_BTTLP = "bottle_price";
    private static final String KEY_PBLNC = "prev_balance";
    private static final String KEY_BLNC = "balance";
    private static final String KEY_BBTTLS = "bonif_bottles";
    private static final String KEY_PBTTLS = "prev_bottles";
    private static final String KEY_BTTLS = "bottles";
    private static final String KEY_SYNC = "sync_date";
    private static final String KEY_MODIF = "modif_date";

    //Movements Table Columns
    private static final String MKEY_ROWID = "numint";
    private static final String MKEY_ID_MOVE = "move_id";
    private static final String MKEY_CLIENT_ID = "client_id";
    private static final String MKEY_PICKUP = "pickup";
    private static final String MKEY_DELIVER = "deliver";
    private static final String MKEY_PAYMENT = "payment";
    private static final String MKEY_MOVE_DATE = "move_date";
    private static final String MKEY_SYNC_DATE = "sync_date";
    private static final String MKEY_FLAG_SYNC = "flag_sync";


    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String CREATE_CLIENTS_TABLE = "CREATE TABLE " + CLIENTS_TABLE + "("
                + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
                + KEY_NAME + " TEXT NOT NULL, "
                + KEY_LOCAL + " TEXT NOT NULL, "
                + KEY_ADDR + " TEXT NOT NULL, "
                + KEY_BTTLP + " REAL NOT NULL, "
                + KEY_PBLNC + " REAL NOT NULL, "
                + KEY_BLNC + " REAL NOT NULL, "
                + KEY_BBTTLS + " INTEGER NOT NULL, "
                + KEY_PBTTLS + " INTEGER NOT NULL, "
                + KEY_BTTLS + " INTEGER NOT NULL, "
                + KEY_SYNC + " TEXT NOT NULL, "
                + KEY_MODIF + " TEXT NOT NULL )";

        String CREATE_MOVEMENTS_TABLE = "CREATE TABLE " + MOVEMENTS_TABLE + "("
                + MKEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
                + MKEY_ID_MOVE + "TEXT NOT NULL"
                + MKEY_CLIENT_ID + " INTEGER NOT NULL, "
                + MKEY_PICKUP + " INTEGER NOT NULL, "
                + MKEY_DELIVER + " INTEGER NOT NULL, "
                + MKEY_PAYMENT + " REAL NOT NULL, "
                + MKEY_MOVE_DATE + " TEXT NOT NULL, "
                + MKEY_SYNC_DATE + " TEXT NOT NULL, "
                + MKEY_FLAG_SYNC + " INTEGER NOT NULL )";

        sqLiteDatabase.execSQL(CREATE_CLIENTS_TABLE);
        sqLiteDatabase.execSQL(CREATE_MOVEMENTS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + CLIENTS_TABLE);
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + MOVEMENTS_TABLE);
        onCreate(sqLiteDatabase);
    }
4

1 回答 1

2

您忘记输入空格和逗号 +MKEY_ID_MOVE + "TEXT NOT NULL"

 String CREATE_MOVEMENTS_TABLE = "CREATE TABLE " + MOVEMENTS_TABLE + "("
                + MKEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
                + MKEY_ID_MOVE + " TEXT NOT NULL,"
                + MKEY_CLIENT_ID + " INTEGER NOT NULL, "
                + MKEY_PICKUP + " INTEGER NOT NULL, "
                + MKEY_DELIVER + " INTEGER NOT NULL, "
                + MKEY_PAYMENT + " REAL NOT NULL, "
                + MKEY_MOVE_DATE + " TEXT NOT NULL, "
                + MKEY_SYNC_DATE + " TEXT NOT NULL, "
                + MKEY_FLAG_SYNC + " INTEGER NOT NULL )";

也可以使用 try CatchsqLiteDatabase.execSQL

try
{
sqLiteDatabase.execSQL(CREATE_CLIENTS_TABLE);
}
Catch(Exception e)
{
}
try
{
 sqLiteDatabase.execSQL(CREATE_MOVEMENTS_TABLE);
}
Catch(Exception e)
{
}
于 2013-08-07T17:58:37.383 回答