无法命名我的sqlite数据库

时间:2017-02-01 18:11:57

标签: android sqlite

所以我有这个简单的SQLite数据库,当我把它命名为“contactsManager”(从我正在关注的一些教程中取名时)它有效但除了那个名称之外没什么可行的我尝试了很多名字(骆驼案例,单词之间的下划线) ,每个单词的第一个字母以大写字母开头,只有一个字,开始小写字母,以大写字母开头)如果我不使用“contactsManager”应用程序将无法工作(我做了另一个项目,这不是我的同一个项目在执行教程的同时)这里是数据库类代码

`package com.bignerdranch.android.test;

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



public class DataBaseHelper extends SQLiteOpenHelper{
    private static final String DATABASE_NAME="contactsManager";
    private static final int DATABASE_VERSION =1;
    private static final String TABLE_NAME="table";
    private static final String COL_NAME="name";

    public DataBaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        SQLiteDatabase db= this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
       String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
                + COL_NAME + " INTEGER PRIMARY KEY,"+")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)      {
        db.execSQL("DROP ABLE IF EXSITS"+TABLE_NAME);
        onCreate(db);
    }

}`

这是主要代码

`package com.bignerdranch.android.test;

package com.bignerdranch.android.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    public Button mButton;
    public EditText mEditText;
    DataBaseHelper DataBase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*mButton=(Button)findViewById(R.id.button);
        mEditText=(EditText)findViewById(R.id.Text);*/
        DataBase= new DataBaseHelper(this);

    }


}`

the output

然后我将名称更改为

 private static final String DATABASE_NAME="Hello";

the application stops

logcat的

02-01 14:00:29.195 8487-8487/? I/art: Late-enabling -Xcheck:jni
02-01 14:00:29.195 8487-8487/? W/art: Unexpected CPU variant for X86 using defaults: x86
02-01 14:00:29.220 8487-8493/? E/art: Failed sending reply to debugger: Broken pipe
02-01 14:00:29.220 8487-8493/? I/art: Debugger is no longer active
02-01 14:00:29.220 8487-8493/? I/art: Starting a blocking GC Instrumentation
02-01 14:00:29.273 8487-8487/? W/System: ClassLoader referenced unknown path: /data/app/com.bignerdranch.android.test-2/lib/x86
02-01 14:00:29.279 8487-8487/? I/InstantRun: Instant Run Runtime started. Android package is com.bignerdranch.android.test, real application class is null.
02-01 14:00:29.845 8487-8487/? W/System: ClassLoader referenced unknown path: /data/app/com.bignerdranch.android.test-2/lib/x86
02-01 14:00:29.995 8487-8487/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
02-01 14:00:30.163 8487-8487/? E/SQLiteLog: (1) near "table": syntax error
02-01 14:00:30.164 8487-8487/? D/AndroidRuntime: Shutting down VM
                                                 
                                                 
                                                 --------- beginning of crash
02-01 14:00:30.167 8487-8487/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.bignerdranch.android.test, PID: 8487
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.test/com.bignerdranch.android.test.MainActivity}: android.database.sqlite.SQLiteException: near "table": syntax error (code 1): , while compiling: CREATE TABLE table(nameTEXT)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:154)
                                                     at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                  Caused by: android.database.sqlite.SQLiteException: near "table": syntax error (code 1): , while compiling: CREATE TABLE table(nameTEXT)
                                                     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1675)
                                                     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1606)
                                                     at com.bignerdranch.android.test.DataBaseHelper.onCreate(DataBaseHelper.java:26)
                                                     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                                     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                     at com.bignerdranch.android.test.DataBaseHelper.<init>(DataBaseHelper.java:18)
                                                     at com.bignerdranch.android.test.MainActivity.onCreate(MainActivity.java:22)
                                                     at android.app.Activity.performCreate(Activity.java:6664)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:154) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

3 个答案:

答案 0 :(得分:1)

以下是您错误中最重要的部分:

  

引起:android.database.sqlite.SQLiteException:near&#34; table&#34 ;:语法错误(代码1):,同时编译:CREATE TABLE表(nameTEXT)

您的问题根本不是数据库名称,而是您的表名。您无法为表格table命名。单词table是SQL中的关键字。为您的表选择一个不同的名称。

此处还有一个错误:您需要在列名称与其类型之间留一个空格。将nameTEXT更改为name TEXT

答案 1 :(得分:0)

错误的听写  @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXSITS"+TABLE_NAME); onCreate(db); }

而不是你写的* able

答案 2 :(得分:0)

我认为你应该替换:

   String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
            + COL_NAME + " INTEGER PRIMARY KEY,"+")";

使用:

   String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "("
            + COL_NAME + " INTEGER PRIMARY KEY"+")";

,

之后删除KEY