SQLite增删改查小程序,运行报错,onClick 报错,已解决

写了一个简单得SQLite增删改查小程序,运行报错,检查了多遍,没检查出来。总是报错。报错如下


05-04 03:09:46.425 3496-3496/? E/libprocessgroup: failed to make and chown /acct/uid_10060: Read-only file system
05-04 03:09:46.425 3496-3496/? W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
05-04 03:09:46.425 3496-3496/? I/art: Late-enabling -Xcheck:jni
05-04 03:09:46.639 3496-3496/com.hh.sqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.hh.sqlite-1/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.hh.sqlite-1@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
05-04 03:09:46.904 3496-3496/com.hh.sqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.hh.sqlite-1/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.hh.sqlite-1@split_lib_slice_0_apk.apk@classes.dex) because non-0 exit status
05-04 03:09:46.944 3496-3496/com.hh.sqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.hh.sqlite-1/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.hh.sqlite-1@split_lib_slice_1_apk.apk@classes.dex) because non-0 exit status
05-04 03:09:46.982 3496-3496/com.hh.sqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.hh.sqlite-1/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.hh.sqlite-1@split_lib_slice_2_apk.apk@classes.dex) because non-0 exit status
05-04 03:09:47.028 3496-3496/com.hh.sqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.hh.sqlite-1/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.hh.sqlite-1@split_lib_slice_3_apk.apk@classes.dex) because non-0 exit status
05-04 03:09:47.082 3496-3496/com.hh.sqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.hh.sqlite-1/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.hh.sqlite-1@split_lib_slice_4_apk.apk@classes.dex) because non-0 exit status
05-04 03:09:47.144 3496-3496/com.hh.sqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.hh.sqlite-1/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.hh.sqlite-1@split_lib_slice_5_apk.apk@classes.dex) because non-0 exit status
05-04 03:09:47.230 3496-3496/com.hh.sqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.hh.sqlite-1/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.hh.sqlite-1@split_lib_slice_6_apk.apk@classes.dex) because non-0 exit status
05-04 03:09:47.285 3496-3496/com.hh.sqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.hh.sqlite-1/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.hh.sqlite-1@split_lib_slice_7_apk.apk@classes.dex) because non-0 exit status
05-04 03:09:47.326 3496-3496/com.hh.sqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.hh.sqlite-1/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.hh.sqlite-1@split_lib_slice_8_apk.apk@classes.dex) because non-0 exit status
05-04 03:09:47.364 3496-3496/com.hh.sqlite W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=x86 --instruction-set-features=smp,-ssse3,-sse4.1,-sse4.2,-avx,-avx2 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=x86 --instruction-set-features=default --dex-file=/data/app/com.hh.sqlite-1/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/x86/data@app@com.hh.sqlite-1@split_lib_slice_9_apk.apk@classes.dex) because non-0 exit status
05-04 03:09:47.366 3496-3496/com.hh.sqlite W/System: ClassLoader referenced unknown path: /data/app/com.hh.sqlite-1/lib/x86
05-04 03:09:47.389 3496-3496/com.hh.sqlite I/InstantRun: starting instant run server: is main process
05-04 03:09:47.495 3496-3537/com.hh.sqlite D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
05-04 03:09:47.779 3496-3537/com.hh.sqlite D/libEGL: loaded /system/lib/egl/libEGL_emulation.so
    loaded /system/lib/egl/libGLESv1_CM_emulation.so
05-04 03:09:47.789 3496-3537/com.hh.sqlite D/libEGL: loaded /system/lib/egl/libGLESv2_emulation.so
05-04 03:09:47.823 3496-3537/com.hh.sqlite I/OpenGLRenderer: Initialized EGL, version 1.4
05-04 03:09:47.953 3496-3537/com.hh.sqlite W/EGL_emulation: eglSurfaceAttrib not implemented
05-04 03:09:47.953 3496-3537/com.hh.sqlite W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe7fbcf00, error=EGL_SUCCESS
05-04 03:09:48.389 3496-3496/com.hh.sqlite I/Choreographer: Skipped 46 frames!  The application may be doing too much work on its main thread.
05-04 03:10:20.564 3496-3537/com.hh.sqlite W/EGL_emulation: eglSurfaceAttrib not implemented
05-04 03:10:20.564 3496-3537/com.hh.sqlite W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe7fbd4e0, error=EGL_SUCCESS
05-04 03:10:21.176 3496-3537/com.hh.sqlite E/Surface: getSlotFromBufferLocked: unknown buffer: 0xf3d57e80
05-04 03:10:27.626 3496-3496/com.hh.sqlite E/SQLiteLog: (1) near "creat": syntax error
05-04 03:10:27.627 3496-3496/com.hh.sqlite D/AndroidRuntime: Shutting down VM
    
    
    --------- beginning of crash
05-04 03:10:27.627 3496-3496/com.hh.sqlite E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.hh.sqlite, PID: 3496
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at android.view.View$DeclaredOnClickListener.onClick(View.java:4452)
        at android.view.View.performClick(View.java:5198)
        at android.view.View$PerformClick.run(View.java:21147)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        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: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
        at android.view.View.performClick(View.java:5198) 
        at android.view.View$PerformClick.run(View.java:21147) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        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 "creat": syntax error (code 1): , while compiling: creat table person(id integer primary key autoincrement,name varchar(20)number varchar(20))
        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.hh.sqlite.PersonSQLiteOpenHelper.onCreate(PersonSQLiteOpenHelper.java:21)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
        at com.hh.sqlite.PersonDao.add(PersonDao.java:17)
        at com.hh.sqlite.MainActivity.add(MainActivity.java:28)
        at java.lang.reflect.Method.invoke(Native Method) 
        at android.view.View$DeclaredOnClickListener.onClick(View.java:4447) 
        at android.view.View.performClick(View.java:5198) 
        at android.view.View$PerformClick.run(View.java:21147) 
        at android.os.Handler.handleCallback(Handler.java:739) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        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) 
05-04 03:10:31.001 3496-3496/com.hh.sqlite I/Process: Sending signal. PID: 3496 SIG: 9

我的程序:ativity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <EditText
        android:id="@+id/et_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="请输入id"
        android:inputType="number" />

    <EditText
        android:id="@+id/et_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="请输入姓名"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>
    <EditText
        android:id="@+id/et_number"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="请输入账号"
        android:inputType="number" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/btn_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="add"
            android:text="保存" />

        <Button
            android:id="@+id/btn_update"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="update"
            android:text="修改" />

        <Button
            android:id="@+id/btn_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="delete"
            android:text="删除" />

        <Button
            android:id="@+id/btn_find"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="find"
            android:text="查找" />

    </LinearLayout>


</LinearLayout>

MainActivity.java

package com.hh.sqlite;

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

public class MainActivity extends Activity {
    private EditText et_id;
    private EditText et_number;
    private EditText et_name;

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

        et_id=(EditText) findViewById(R.id.et_id);
        et_name=(EditText) findViewById(R.id.et_name);
        et_number=(EditText)findViewById(R.id.et_number);

    }

    public void add(View view) {
        PersonDao personDao=new PersonDao(this);
        long id=personDao.add(et_name.getText().toString().trim(),et_number.getText().toString().trim());//有问题
        Toast.makeText(this,"添加信息为,id为"+id , Toast.LENGTH_SHORT).show();


    }
    public void update(View view) {
        PersonDao personDao = new PersonDao(this);
        int count = personDao.update(Long.parseLong(et_id.getText().toString().trim()),et_name.getText().toString().trim(), et_number.getText().toString().trim());
        Toast.makeText(this, "成功修改"+count+"条信息", Toast.LENGTH_SHORT).show();

    }
    public void delete(View view) {
        PersonDao personDao = new PersonDao(this);
        int count = personDao.delete(Long.parseLong(et_id.getText().toString().trim()));
        Toast.makeText(this, "成功删除"+count+"条信息", Toast.LENGTH_SHORT).show();

    }
    public void find(View view) {
        PersonDao personDao = new PersonDao(this);
        boolean result = personDao.find(Long.parseLong(et_id.getText().toString().trim()));
        if(result){
            Toast.makeText(this, "查询信息成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this, "查询信息失败", Toast.LENGTH_SHORT).show();
        }

    }
}

PersonDao.java

package com.hh.sqlite;

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

public class PersonDao {
    private PersonSQLiteOpenHelper helper;

    public PersonDao(Context context) {//构造方法
        helper = new PersonSQLiteOpenHelper(context);
    }

    public long add(String name,String number){
        //错误修改前:下面这句话报错-,修改后无错误
        SQLiteDatabase db = helper.getWritableDatabase();//SQLiteDatabase 类, 该类是一个数据库访问类,封装了一系列数据库操作的API,可以对数据进行增删改查操作。

        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("number",number);
        long id = db.insert("person", null, values);//调用insert 方法,table 是person 表格
        db.close();
        return id;//通过调用add得到他的id 值
    }
    public int update(long id,String name,String number){
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("number", number);
        int count = db.update("person", values, "id=?", new String[]{String.valueOf(id)});
        db.close();
        return count;
    }

    public int delete(long id){
        SQLiteDatabase db = helper.getWritableDatabase();
        int count = db.delete("person","id=?",new String[]{String.valueOf(id)});
        db.close();
        return count;
    }

    public boolean find(long id){
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("person", null, "id=?", new String[]{String.valueOf(id)}, null, null, null);
        boolean result = cursor.moveToNext();
        cursor.close();
        db.close();
        return result;
    }
}

PersonSQLiteOpenHelper.java

package com.hh.sqlite;

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

public class PersonSQLiteOpenHelper extends SQLiteOpenHelper {
    //还需要写一个构造方法,需要调用父类的构造方法
    public PersonSQLiteOpenHelper(Context context){//需要上下文所以添加Context

        super(context,"person.db",null,1);//我们要创建的数据库名称person.db;版本号为1;游标null

    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        StringBuffer sq1=new StringBuffer();//创建了一个字符串对象
        sq1.append("creat table person(");//!!!!!!!!!!!!!!!根本错误第一个错误将creat改为create!!!!
        sq1.append("id integer primary key autoincrement,");
        sq1.append("name varchar(20)");//!!!!!!!!!!第二个错误!!!!!!!!!!
        sq1.append("number varchar(20))");
        db.execSQL(sq1.toString());//错误修改前:这句话报错-,修改后无错误
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("alter table person add account varchar(20) default 1000 ");//更新的时候,添加了一个account 字段

    }
}

Person.java

package com.hh.sqlite;

public class Person {
    private long id;
    private String name;
    private String number;

    public Person(long id, String name, String number) {
        super();
        this.id = id;
        this.name = name;
        this.number = number;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

}


错误:在文中标出!

运算结果:





©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页