Java android studio中缺少数据库错误?

Java android studio中缺少数据库错误?,java,android,sqlite,Java,Android,Sqlite,我几乎是在复制和粘贴来自新boston android系列的SQLite教程。我做了他所做的一切,但当我编译控制台时,它告诉我我有语法错误,或者数据库丢失了。我很确定我没有语法错误。这是我所有的代码和控制台消息 MainActivity.java import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Edi

我几乎是在复制和粘贴来自新boston android系列的SQLite教程。我做了他所做的一切,但当我编译控制台时,它告诉我我有语法错误,或者数据库丢失了。我很确定我没有语法错误。这是我所有的代码和控制台消息

MainActivity.java

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

public class MainActivity extends AppCompatActivity {

    EditText input;
    TextView productText;
    MyDBHandler dbHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        input = (EditText)findViewById(R.id.editText_ID);
        productText = (TextView)findViewById(R.id.textOutput_ID);
        dbHandler = new MyDBHandler(this, null, null, 1);
        printDatabase();
    }

    //add a product to the database
    public void addButtonClicked(View view){
        Products product = new Products(input.getText().toString());
        dbHandler.addProduct(product);
        printDatabase();
    }

    //delete items
    public void deleteButtonClicked(View view){
        String inputText = input.getText().toString();
        dbHandler.deleteProduct(inputText);
        printDatabase();
    }

    public void printDatabase(){
        String dbString = dbHandler.databaseToString();
        productText.setText(dbString);
        input.setText("");
    }
}
MyDBHandler.java

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


public class MyDBHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "products.db";
    public static final String TABLE_PRODUCTS = "products";
    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_PRODUCTNAME = "productname";

    public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE_TABLE " + TABLE_PRODUCTS + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
                COLUMN_PRODUCTNAME + " TEXT " +
                ");";
        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP_TABLE IF EXISTS " + TABLE_PRODUCTS);
        onCreate(db);
    }

    //add a new row to the database
    public void addProduct(Products product){
        ContentValues values = new ContentValues();
        values.put(COLUMN_PRODUCTNAME, product.get_productname());
        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE_PRODUCTS, null, values);
        db.close();
    }

    //delete a product from the database
    public void deleteProduct(String productName){
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("DELETE_FROM " + TABLE_PRODUCTS + " WHERE " + COLUMN_PRODUCTNAME + "=\"" + productName + "\";" );
    }

    //print out the database as a string
    public String databaseToString(){
        String dbString = "";
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";

        //Cursor point to location in your results
        Cursor c = db.rawQuery(query, null);
        //Move to the first row in your results
        c.moveToFirst();

        while(!c.isAfterLast()){
            if(c.getString(c.getColumnIndex("productname")) != null){
                dbString += c.getString(c.getColumnIndex("productname"));
                dbString += "\n";
            }
        }
        db.close();
        return dbString;
    }
}
Products.java

public class Products {
    private int _id;
    private String _productname;

    public Products(){

    }

    public Products(String productname){
        this._productname = productname;
    }

    public void set_id(int _id) {
        this._id = _id;
    }

    public void set_productname(String _productname) {
        this._productname = _productname;
    }

    public int get_id() {
        return _id;
    }

    public String get_productname() {
        return _productname;
    }
}
控制台消息

D/RelationGraph: garbageCollect()
W/ResourcesManager: getTopLevelResources: /data/app/com.example.emilythacker.myapplication-1/base.apk / 1.0 running in com.example.emilythacker.myapplication rsrc of package com.example.emilythacker.myapplication
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
E/SQLiteLog: (1) near "CREATE_TABLE": syntax error
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.emilythacker.myapplication, PID: 2125
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.emilythacker.myapplication/com.example.emilythacker.myapplication.MainActivity}: android.database.sqlite.SQLiteException: near "CREATE_TABLE": syntax error (code 1): , while compiling: CREATE_TABLE products(_id INTEGER PRIMARY KEY AUTOINCREMENT productname TEXT );
                  #################################################################
                  Error Code : 1 (SQLITE_ERROR)
                  Caused By : SQL(query) error or missing database.
                    (near "CREATE_TABLE": syntax error (code 1): , while compiling: CREATE_TABLE products(_id INTEGER PRIMARY KEY AUTOINCREMENT productname TEXT );)
                  #################################################################
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
                      at android.app.ActivityThread.access$1100(ActivityThread.java:221)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:158)
                      at android.app.ActivityThread.main(ActivityThread.java:7224)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                   Caused by: android.database.sqlite.SQLiteException: near "CREATE_TABLE": syntax error (code 1): , while compiling: CREATE_TABLE products(_id INTEGER PRIMARY KEY AUTOINCREMENT productname TEXT );
                  #################################################################
                  Error Code : 1 (SQLITE_ERROR)
                  Caused By : SQL(query) error or missing database.
                    (near "CREATE_TABLE": syntax error (code 1): , while compiling: CREATE_TABLE products(_id INTEGER PRIMARY KEY AUTOINCREMENT productname TEXT );)
                  #################################################################
                      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1058)
                      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:623)
                      at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                      at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
                      at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                      at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1812)
                      at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1743)
                      at com.example.emilythacker.myapplication.MyDBHandler.onCreate(MyDBHandler.java:32)
                      at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                      at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                      at com.example.emilythacker.myapplication.MyDBHandler.databaseToString(MyDBHandler.java:59)
                      at com.example.emilythacker.myapplication.MainActivity.printDatabase(MainActivity.java:41)
                      at com.example.emilythacker.myapplication.MainActivity.onCreate(MainActivity.java:23)
                      at android.app.Activity.performCreate(Activity.java:6876)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)
                        ... 9 more
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'
D/RelationGraph:garbageCollect()
W/ResourcesManager:gettoplavelresources:/data/app/com.example.emilythacker.myapplication-1/base.apk/1.0运行在com.example.emilythacker.myapplication包com.example.emilythacker.myapplication的com.example.myapplication rsrc中
W/art:Android 4.1之前的Android.graphics.PorterDuffColorFilter方法Android.support.graphics.drawable.VectorDrawableCompat.UpdatentFilter(Android.graphics.PorterDuffColorFilter,Android.content.res.ColorStateList,Android.graphics.PorterDuff$模式)将错误地重写android.graphics.drawable.drawable中的包私有方法
E/SQLiteLog:(1)靠近“CREATE_TABLE”:语法错误
D/AndroidRuntime:关闭虚拟机
E/AndroidRuntime:致命异常:主
进程:com.example.emilythacker.myapplication,PID:2125
java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.emilythacker.myapplication/com.example.emilythacker.myapplication.MainActivity}:android.database.sqlite.SQLiteException:near“CREATE_TABLE”:编译时语法错误(代码1)::CREATE_TABLE products(_idinteger主键AUTOINCREMENT productname TEXT);
#################################################################
错误代码:1(SQLITE_错误)
原因:SQL(查询)错误或缺少数据库。
(在“CREATE_TABLE”附近:编译时出现语法错误(代码1):CREATE_TABLE产品(_idinteger主键AUTOINCREMENT productname TEXT);)
#################################################################
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)上
位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
在android.app.ActivityThread.access$1100(ActivityThread.java:221)
在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1794)
位于android.os.Handler.dispatchMessage(Handler.java:102)
位于android.os.Looper.loop(Looper.java:158)
位于android.app.ActivityThread.main(ActivityThread.java:7224)
位于java.lang.reflect.Method.invoke(本机方法)
在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run上(ZygoteInit.java:1230)
位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
原因:android.database.sqlite.sqlite异常:在“创建\u表”附近:编译时出现语法错误(代码1):创建\u表产品(\u id INTEGER主键AUTOINCREMENT productname TEXT);
#################################################################
错误代码:1(SQLITE_错误)
原因:SQL(查询)错误或缺少数据库。
(在“CREATE_TABLE”附近:编译时出现语法错误(代码1):CREATE_TABLE产品(_idinteger主键AUTOINCREMENT productname TEXT);)
#################################################################
位于android.database.sqlite.SQLiteConnection.nativePrepareStatement(本机方法)
位于android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1058)
位于android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:623)
位于android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
位于android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:59)
位于android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31)
位于android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1812)
位于android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1743)
位于com.example.emilythacker.myapplication.MyDBHandler.onCreate(MyDBHandler.java:32)
位于android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
位于android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
位于com.example.emilythacker.myapplication.MyDBHandler.databaseToString(MyDBHandler.java:59)
在com.example.emilythacker.myapplication.MainActivity.printDatabase(MainActivity.java:41)上
位于com.example.emilythacker.myapplication.MainActivity.onCreate(MainActivity.java:23)
位于android.app.Activity.performCreate(Activity.java:6876)
位于android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3206)上
... 9更多
已断开与目标VM的连接,地址:'localhost:8600',传输:'socket'

自动递增后缺少逗号。创建表代码应为

String query = "CREATE_TABLE " + TABLE_PRODUCTS + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COLUMN_PRODUCTNAME + " TEXT " +
            ");";

另请参见Abihsek的回答,“创建表”也是错误的。

堆栈跟踪正好指向问题

Caused by: android.database.sqlite.SQLiteException: near "CREATE_TABLE"
您的查询应该是
CREATE TABLE
,而不是不带下划线的
CREATE\u TABLE

我看到你在使用下划线,如果更多的查询

DROP\u TABLE
应该是
DROP TABLE

DELETE\u FROM
应该是
DELETE FROM