无法创建本地数据库SQLite

时间:2014-12-09 13:22:42

标签: android database sqlite exception localdb

我试图在Android SQLite上创建一个本地数据库,但每次我尝试创建我在我的logcat上得到这个异常,为什么会发生这种情况?我已经删除了应用程序并以新的方式运行...

private static final int DATABASE_VERSAO = 1;
public static final String DATABASE_NAME = "STARA_DB";

private static final String TB_CLIENTE = "Table de Clientes";

private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase database;
private static final String TAG = "DBAdapter";

 String TabelaClientes = "CREATE TABLE " 
        + TB_CLIENTE 
        + "(ID_CLIENTE  PRIMARY KEY NOT NULL AUTOINCREMENT, " 
        + "KUNNR TEXT, " 
        + "NAME1 TEXT );";

public ClienteDAO(Context ctx) {
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSAO);
    }

    public void onCreate(SQLiteDatabase database) {
        try {
            database.execSQL(TB_CLIENTE);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");

        database.execSQL("DROP TABLE IF EXISTS" + TB_CLIENTE);
        // onCreate(database);
        onUpgrade(database, oldVersion, newVersion);
    }
public long insertCliente(String KUNNR, String NAME1) {
    ContentValues initialValuesClientes = new ContentValues();
    initialValuesClientes.put("KUNNR", KUNNR);
    initialValuesClientes.put("NAME1", NAME1);

    return database.insert(TB_CLIENTE, null, initialValuesClientes);
}
public List<ClienteModelo> getListaClientes() {

    List<ClienteModelo> clientes = new ArrayList<ClienteModelo>();

    Cursor cCli = DBHelper.getReadableDatabase().rawQuery("Select KUNNR, NAME1 from " + TB_CLIENTE + ";", null);

    while (cCli.moveToNext()) {
        ClienteModelo cliente = new ClienteModelo();

        // cliente.setClienteID(cCli.getLong(cCli.getColumnIndex("ID_CLIENTE")));
        cliente.setKunnr(cCli.getString(cCli.getColumnIndex("KUNNR")));
        cliente.setName1(cCli.getString(cCli.getColumnIndex("NAME1")));

        clientes.add(cliente);
    }

    cCli.close();
    return clientes;

}

public void deleteCliente() {
    database.delete(TB_CLIENTE, null, null);
}

LogCat上的消息:

no such table: TABLE_CLIENTE
java.lang.RuntimeException: Unable to start activity    ComponentInfo{stara.formularios/stara.formularios.views.FormNota}: android.database.sqlite.SQLiteException: no such table: TABLE_CLIENTE (code 1): , while compiling: Select KUNNR, NAME1 from TABLE_CLIENTE;
Caused by: android.database.sqlite.SQLiteException: no such table: TABLE_CLIENTE (code 1): , while compiling: Select KUNNR, NAME1 from TABLE_CLIENTE;"

2 个答案:

答案 0 :(得分:5)

您忽略的CREATE TABLE中存在语法错误。

  • "Table de Clientes"应该是一个有效的标识符,例如您之后使用的"TABLE_CLIENTE",根据您的堆栈跟踪。

  • ID_CLIENTE PRIMARY KEY NOT NULL AUTOINCREMENT需要INTEGER,例如ID_CLIENTE INTEGER PRIMARY KEY AUTOINCREMENTNOT NULL是多余的;主键不能为空。

  • 您需要实际将SQL而不是表名传递给execSQL()

  • 删除try中的catch - onCreate()。回调需要抛出错误而不是忽略问题。

更改完成后,请卸载您的应用,以便删除旧的数据库文件。

答案 1 :(得分:1)

在Oncreate中你传递了错误的参数来创建表:

public void onCreate(SQLiteDatabase database) {
        try {
            database.execSQL(TabelaClientes);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

表名不应包含任何空格。

private static final String TB_CLIENTE = "Table_de_Clientes";