我的应用程序已停止工作(SQLite)

时间:2016-04-22 10:50:47

标签: java android sqlite android-sqlite

我是Android开发的新手,我正在尝试使用SQLite创建数据库,我一直试图解决这个问题几个小时,但我不知道问题出在哪里。

这是Logcat中出现的内容。

04-22 20:40:11.801 3134-3134/? D/AndroidRuntime: >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<<
04-22 20:40:11.802 3134-3134/? D/AndroidRuntime: CheckJNI is ON
04-22 20:40:11.813 3134-3134/? D/ICU: No timezone override file found: /data/misc/zoneinfo/current/icu/icu_tzdata.dat
04-22 20:40:11.825 3134-3134/? E/memtrack: Couldn't load memtrack module (No such file or directory)
04-22 20:40:11.825 3134-3134/? E/android.os.Debug: failed to load memtrack module: -2
04-22 20:40:11.826 3134-3134/? I/Radio-JNI: register_android_hardware_Radio DONE
04-22 20:40:11.834 3134-3134/? D/AndroidRuntime: Calling main entry com.android.commands.am.Am
04-22 20:40:11.836 1300-1816/? I/ActivityManager: Force stopping com.example.carlos.assigment appid=10057 user=0: from pid 3134
04-22 20:40:11.836 1300-1816/? I/ActivityManager: Killing 3109:com.example.carlos.assigment/u0a57 (adj 9): stop com.example.carlos.assigment
04-22 20:40:11.854 1300-1481/? W/ActivityManager: Spurious death for ProcessRecord{f73785a 0:com.example.carlos.assigment/u0a57}, curProc for 3109: null
04-22 20:40:11.858 3134-3134/? D/AndroidRuntime: Shutting down VM
04-22 20:40:11.863 1300-2975/? E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa1373490
04-22 20:40:11.869 1300-1498/? W/InputMethodManagerService: Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@3e0c026 attribute=null, token = android.os.BinderProxy@f5dce74
04-22 20:40:12.043 1300-1319/? W/AppOps: Finishing op nesting under-run: uid 1000 pkg android code 24 time=0 duration=0 nesting=0
04-22 20:40:12.363 3147-3147/? D/AndroidRuntime: >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<<
04-22 20:40:12.364 3147-3147/? D/AndroidRuntime: CheckJNI is ON
04-22 20:40:12.372 3143-3143/? D/AndroidRuntime: >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<<
04-22 20:40:12.373 3143-3143/? D/AndroidRuntime: CheckJNI is ON
04-22 20:40:12.383 3143-3143/? D/ICU: No timezone override file found: /data/misc/zoneinfo/current/icu/icu_tzdata.dat
04-22 20:40:12.394 3147-3147/? D/ICU: No timezone override file found: /data/misc/zoneinfo/current/icu/icu_tzdata.dat
04-22 20:40:12.405 3143-3143/? E/memtrack: Couldn't load memtrack module (No such file or directory)
04-22 20:40:12.405 3143-3143/? E/android.os.Debug: failed to load memtrack module: -2
04-22 20:40:12.406 3143-3143/? I/Radio-JNI: register_android_hardware_Radio DONE
04-22 20:40:12.418 3147-3147/? E/memtrack: Couldn't load memtrack module (No such file or directory)
04-22 20:40:12.418 3147-3147/? E/android.os.Debug: failed to load memtrack module: -2
04-22 20:40:12.419 3147-3147/? I/Radio-JNI: register_android_hardware_Radio DONE
04-22 20:40:12.423 3143-3143/? D/AndroidRuntime: Calling main entry com.android.commands.wm.Wm
04-22 20:40:12.424 3143-3143/? D/AndroidRuntime: Shutting down VM
04-22 20:40:12.435 3147-3147/? D/AndroidRuntime: Calling main entry com.android.commands.am.Am
04-22 20:40:12.438 1300-1497/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.carlos.assigment/.MainActivity} from uid 0 on display 0
04-22 20:40:12.439 3143-3159/? E/art: Thread attaching while runtime is shutting down: Binder_2
04-22 20:40:12.439 3143-3159/? I/AndroidRuntime: NOTE: attach of thread 'Binder_2' failed
04-22 20:40:12.449 3147-3147/? D/AndroidRuntime: Shutting down VM
04-22 20:40:12.461 3162-3162/? I/art: Not late-enabling -Xcheck:jni (already on)
04-22 20:40:12.470 1300-1495/? I/ActivityManager: Start proc 3162:com.example.carlos.assigment/u0a57 for activity com.example.carlos.assigment/.MainActivity
04-22 20:40:12.508 3162-3162/? W/System: ClassLoader referenced unknown path: /data/app/com.example.carlos.assigment-1/lib/x86
04-22 20:40:12.545 2305-2337/? E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb4092ee0
04-22 20:40:12.585 3162-3162/? E/SQLiteLog: (1) near "TABLEtable": syntax error
04-22 20:40:12.586 3162-3162/? D/AndroidRuntime: Shutting down VM
04-22 20:40:12.586 3162-3162/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: com.example.carlos.assigment, PID: 3162
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.carlos.assigment/com.example.carlos.assigment.MainActivity}: android.database.sqlite.SQLiteException: near "TABLEtable": syntax error (code 1): , while compiling: CREATE TABLEtable(Conductivity FLOATOxygen INTEGERpHFLOAT usernameSTRING PRIMARY KEY passwordTEXTmoisture INTEGER );
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:148)
                                                     at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                  Caused by: android.database.sqlite.SQLiteException: near "TABLEtable": syntax error (code 1): , while compiling: CREATE TABLEtable(Conductivity FLOATOxygen INTEGERpHFLOAT usernameSTRING PRIMARY KEY passwordTEXTmoisture INTEGER );
                                                     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
                                                     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
                                                     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:1674)
                                                     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
                                                     at com.example.carlos.assigment.DBAdapter.onCreate(DBAdapter.java:43)
                                                     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                                     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                     at com.example.carlos.assigment.DBAdapter.databaseToString(DBAdapter.java:75)
                                                     at com.example.carlos.assigment.MainActivity.printDatabase(MainActivity.java:62)
                                                     at com.example.carlos.assigment.MainActivity.onCreate(MainActivity.java:40)
                                                     at android.app.Activity.performCreate(Activity.java:6237)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:148) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
04-22 20:40:12.587 1300-1481/? W/ActivityManager:   Force finishing activity com.example.carlos.assigment/.MainActivity
04-22 20:40:12.601 952-952/? E/EGL_emulation: tid 952: eglCreateSyncKHR(1294): error 0x3004 (EGL_BAD_ATTRIBUTE)
04-22 20:40:12.685 1300-2975/? I/OpenGLRenderer: Initialized EGL, version 1.4
04-22 20:40:12.707 1300-2975/? W/EGL_emulation: eglSurfaceAttrib not implemented
04-22 20:40:12.707 1300-2975/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9c3e5960, error=EGL_SUCCESS
04-22 20:40:13.121 1300-1314/? W/ActivityManager: Activity pause timeout for ActivityRecord{4a19467 u0 com.example.carlos.assigment/.MainActivity t6 f}
04-22 20:40:13.162 2305-2337/? W/EGL_emulation: eglSurfaceAttrib not implemented
04-22 20:40:13.162 2305-2337/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa0975000, error=EGL_SUCCESS

这是我的MainActivity

package com.example.carlos.assigment;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText exampleInput;
    TextView exampletext;
    DBAdapter dbHandler;
    EditText exampleInput1;
    EditText exampleInput2;
    EditText exampleInput3;
    EditText exampleInput4;
    EditText exampleInput5;;


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

        exampleInput =(EditText)findViewById(R.id.exampleInput);
        exampleInput1 =(EditText)findViewById(R.id.exampleInput1);
        exampleInput2 =(EditText)findViewById(R.id.exampleInput2);
        exampleInput3 =(EditText)findViewById(R.id.exampleInput3);
        exampleInput4 =(EditText)findViewById(R.id.exampleInput4);
        exampleInput5 =(EditText)findViewById(R.id.exampleInput5);
        exampletext = (TextView) findViewById(R.id.exampletext);
        dbHandler = new DBAdapter(this, null, null, 1);
        printDatabase();}

    public void addButtonClicked(View view){
        DatabaseList data = new DatabaseList(exampleInput.getText().toString(),
                exampleInput2.getText().toString(),
                exampleInput1.getText().toString(),
                Integer.parseInt(exampleInput3.getText().toString()),
                Float.valueOf(exampleInput4.getText().toString()),
                Float.valueOf(exampleInput5.getText().toString()));
        dbHandler.addData(data);
        printDatabase();
        dbHandler.deleteData();
    }

    public void deleteButtonClicked(){

    }




    public void printDatabase(){
        String dbString = dbHandler.databaseToString();
        exampletext.setText(dbString);
        exampleInput.setText("");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是我执行SQL操作的地方

package com.example.carlos.assigment;

import android.content.*;
import android.database.*;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBAdapter extends SQLiteOpenHelper {



    private static final int DATA_VERSION = 1;
    private static final String DATABASE_NAME = "database.db";
    public static final String TABLE = "table";
    public static final String COLUMN_CONDUCTIVITY ="Conductivity";
    public static final String COLUMN_OXYGEN = "Oxygen";
    public static final String COLUMN_PH = "pH";
    public static final String COLUMN_USERNAME = "username";
    public static final String COLUMN_PASSWORD = "password";
    public static final String COLUMN_MOISTURE = "moisture";

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

    public DBAdapter(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) {
        super(context, name, factory, version, errorHandler);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String query = "CREATE TABLE" + TABLE + "(" +
                COLUMN_CONDUCTIVITY+ " FLOAT" +
                COLUMN_OXYGEN+ " INTEGER" +
                COLUMN_PH+ "FLOAT " +
                COLUMN_USERNAME+ "STRING PRIMARY KEY " +
                COLUMN_PASSWORD + "TEXT" +
                COLUMN_MOISTURE+ " INTEGER "+
                ");";

        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP_TABLE_IF_EXITS" + TABLE);
        onCreate(db);
    }
    //Add a new row to the database
    public void addData(DatabaseList data){
        ContentValues values = new ContentValues();
        values.put(COLUMN_CONDUCTIVITY, data.getConductivity());

        values.put(COLUMN_OXYGEN, data.getDate());
        values.put(COLUMN_OXYGEN, data.getOxygen());
        values.put(COLUMN_PASSWORD, data.getPassword());
        values.put(COLUMN_USERNAME, data.getUsername());
        values.put(COLUMN_PH, data.getpH());

        SQLiteDatabase db = getWritableDatabase();
        db.insert(TABLE, null, values);
        db.close();
    }
    //Delete data

    public void deleteData(){
        SQLiteDatabase db = getWritableDatabase();
        db.execSQL("DROP TABLE IF EXIST, TABLE");
    }
    public String databaseToString(){

        String dbString = "";
        SQLiteDatabase db = getWritableDatabase();
        String query = "SELECT * FROM" + TABLE + "WHERE 1";
        Cursor c = db.rawQuery(query,null);
        c.moveToFirst();
        while(!c.isAfterLast()){
            if(c.getString(c.getColumnIndex("Conductivity"))!= null){
                dbString += c.getString(c.getColumnIndex(("Conductivity")));
                dbString += "\n";
            }
        }
        db.close();
        return dbString;

    }
}

这是我的数据库列表

package com.example.carlos.assigment;

/**
 * Created by Carlos on 22/04/2016.
 */
public class DatabaseList {





        private String username;
        private String password;
        private float conductivity;
        private float pH;
        private int oxygen;
        private String date;

        public DatabaseList(){}

        public DatabaseList(String username, String password, String date, int oxygen, float pH, float conductivity){
            this.conductivity = conductivity;
            this.date = date;
            this.oxygen = oxygen;
            this.password = password;
            this.pH = pH;
            this.username = username;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public float getConductivity() {
            return conductivity;
        }

        public void setConductivity(float conductivity) {
            this.conductivity = conductivity;
        }

        public float getpH() {
            return pH;
        }

        public void setpH(float pH) {
            this.pH = pH;
        }

        public int getOxygen() {
            return oxygen;
        }

        public void setOxygen(int oxygen) {
            this.oxygen = oxygen;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }
    }

这是我的主要内容布局

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.carlos.assigment1.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/addButton"
        android:layout_marginTop="48dp"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="45dp"
        android:layout_marginStart="45dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="delete"
        android:id="@+id/deleteButton"
        android:layout_alignTop="@+id/addButton"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="61dp"
        android:layout_marginEnd="61dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/exampleInput1"
        android:layout_above="@+id/exampleInput2"
        android:layout_toRightOf="@+id/exampleInput"
        android:layout_toEndOf="@+id/exampleInput" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/exampleInput2"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/addButton"
        android:layout_toEndOf="@+id/addButton" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/exampleInput3"
        android:layout_marginTop="13dp"
        android:layout_below="@+id/exampleInput2"
        android:layout_toRightOf="@+id/exampleInput5"
        android:layout_toEndOf="@+id/exampleInput5" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/exampleInput4"
        android:layout_below="@+id/exampleInput3"
        android:layout_toRightOf="@+id/addButton"
        android:layout_toEndOf="@+id/addButton"
        android:layout_marginTop="25dp" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/exampleInput5"
        android:layout_marginTop="16dp"
        android:layout_below="@+id/exampleInput4"
        android:layout_toLeftOf="@+id/exampleInput4"
        android:layout_toStartOf="@+id/exampleInput4" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/exampleInput"
        android:layout_above="@+id/exampletext"
        android:layout_alignRight="@+id/addButton"
        android:layout_alignEnd="@+id/addButton" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/exampletext"
        android:layout_alignTop="@+id/exampleInput1"
        android:layout_toRightOf="@+id/deleteButton"
        android:layout_toEndOf="@+id/deleteButton" />
</RelativeLayout>

我真的陷入了困境并且没有想法。

3 个答案:

答案 0 :(得分:1)

将您的查询修复为SQLiteOpenHelper.onCreate。缺少一些空间:

Caused by: android.database.sqlite.SQLiteException: near "TABLEtable":
   syntax error (code 1): , while compiling: 
   CREATE TABLEtable(Conductivity FLOATOxygen INTEGERpHFLOAT usernameSTRING PRIMARY KEY passwordTEXTmoisture INTEGER );

答案 1 :(得分:0)

对于初学者,您需要处理您的创建声明......

"dashed"

最好记录原始查询,以便将它们作为SQL查询读取,然后更容易在查询形式中发现错误。

答案 2 :(得分:0)

编辑像这样的

代码

 String query = "CREATE TABLE" + TABLE + "( " +
            COLUMN_CONDUCTIVITY + " FLOAT " +
            COLUMN_OXYGEN + " INTEGER " +
            COLUMN_PH + " FLOAT " +
            COLUMN_USERNAME + " STRING PRIMARY KEY " +
            COLUMN_PASSWORD + " TEXT" +
            COLUMN_MOISTURE + " INTEGER " +
            " );";