Android,编译时出错,sqlite

时间:2013-09-06 00:28:33

标签: android sqlite logging

我正在尝试创建一个简单的数据库,但看起来它不会编译,可能是语法错误,我看不到......

package com.amf.stockliste;

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

public class StockDatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "stock";

    private static final int DATABASE_VERSION = 1;

    // Instruction pour la création de la BD
    private static final String DATABASE_CREATE = "CREATE TABLE stock (_id INTEGER PRIMARY KEY AUTOINCREMENT, _symbole TEXT NOT NULL, _nom REAL, _valeur REAL, _quantite REAL, _description TEXT NOT NULL);";


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

    // Méthode appelée lors de la creation de la BD
    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL("DATABASE_CREATE");
    }

    // Méthode appelée lors de la mise-à-jour de la BD
        @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        database.execSQL("DROP TABLE IF EXISTS stock");
        // Recreate table
        onCreate(database);
    }
}


public class StockDbAdapter {

    // Database fields
    public static final String KEY_ROWID = "_id";
    public static final String KEY_SYMBOLE = "symbole";
    public static final String KEY_COMPAGNIE = "compagnie";
    public static final String KEY_PRIX = "prix";
    public static final String KEY_QUANTITE = "quantite";
    public static final String KEY_DESCRIPTION = "description";
    private static final String DATABASE_TABLE = "stock";
    private Context context;
    private SQLiteDatabase database;
    private StockDatabaseHelper dbHelper;


    public StockDbAdapter(Context context) {
        dbHelper = new StockDatabaseHelper(context);
        database = dbHelper.getWritableDatabase();
    }

    public SQLiteDatabase open() throws SQLException {
        database = dbHelper.getWritableDatabase();
        return database;
    }
[...]

在MainActivity中我做了类似的事情:

私人StockDbAdapter dbAdapter; dbAdapter = new StockDbAdapter(getActivity());

09-05 20:27:20.658: E/SQLiteLog(7445): (1) near "DATABASE_CREATE": syntax error
09-05 20:27:20.658: D/AndroidRuntime(7445): Shutting down VM
09-05 20:27:20.658: W/dalvikvm(7445): threadid=1: thread exiting with uncaught exception (group=0x40c9c930)
09-05 20:27:20.666: E/AndroidRuntime(7445): FATAL EXCEPTION: main
09-05 20:27:20.666: E/AndroidRuntime(7445): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.amf.stockliste/com.amf.stockliste.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.os.Looper.loop(Looper.java:137)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.app.ActivityThread.main(ActivityThread.java:5039)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at java.lang.reflect.Method.invokeNative(Native Method)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at java.lang.reflect.Method.invoke(Method.java:511)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at dalvik.system.NativeStart.main(Native Method)
09-05 20:27:20.666: E/AndroidRuntime(7445): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.app.Activity.setContentView(Activity.java:1881)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at com.amf.stockliste.MainActivity.onCreate(MainActivity.java:16)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.app.Activity.performCreate(Activity.java:5104)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-05 20:27:20.666: E/AndroidRuntime(7445):     ... 11 more
09-05 20:27:20.666: E/AndroidRuntime(7445): Caused by: android.database.sqlite.SQLiteException: near "DATABASE_CREATE": syntax error (code 1): , while compiling: DATABASE_CREATE
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1663)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1594)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at com.amf.stockliste.StockDatabaseHelper.onCreate(StockDatabaseHelper.java:23)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at com.amf.stockliste.StockDbAdapter.<init>(StockDbAdapter.java:29)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at com.amf.stockliste.StockListe.ajouterDonnees(StockListe.java:79)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at com.amf.stockliste.StockListe.onCreate(StockListe.java:40)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.app.Fragment.performCreate(Fragment.java:1673)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:854)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1137)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.app.Activity.onCreateView(Activity.java:4717)
09-05 20:27:20.666: E/AndroidRuntime(7445):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
09-05 20:27:20.666: E/AndroidRuntime(7445):     ... 21 more

提前谢谢!

1 个答案:

答案 0 :(得分:1)

替换:

database.execSQL("DATABASE_CREATE");

使用:

database.execSQL(DATABASE_CREATE);