本文介绍了Android应用程序崩溃的调用SQLiteHelper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用本教程的某些部分,但我不得不做出一些变化不大和手机我的应用程序崩溃。有在code没有错误,但!

 公共类SQLiteDbHelper扩展SQLiteOpenHelper {
    公共静态最后弦乐TABLE_NAME =表;
    公共静态最后弦乐COLUMN_NAME_SP =SP;
    公共静态最后弦乐COLUMN_NAME_ST =ST;    //如果更改了数据库架构,则必须增加数据库版本。
    公共静态最终诠释DATABASE_VERSION = 1;
    公共静态最后弦乐DATABASE_NAME =SPT.db;    私有静态最后弦乐DATABASE_CREATE =
        CREATE TABLE+ TABLE_NAME +
         (+
        COLUMN_NAME_SP +文本不为空+
        COLUMN_NAME_ST +文本不为空+
        );    公共SQLiteDbHelper(上下文的背景下){
        超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
    }    @覆盖
    公共无效的onCreate(SQLiteDatabase数据库){
        database.execSQL(DATABASE_CREATE);
    }    @覆盖
    公共无效onUpgrade(SQLiteDatabase数据库,诠释oldVersion,诠释静态网页){
      Log.w(SQLiteDbHelper.class.getName(),
          + oldVersion +到从版本升级数据库
              + NEWVERSION +,这将摧毁所有旧数据);
      database.execSQL(DROP TABLE IF EXISTS+ TABLE_NAME);
    }}

然后,后来,我有这样的code为按钮点击的方法:

 公共无效保存(查看视图){
            串ST = editTextSt.getText()的toString()。
            。字符串SP = editTextSp.getText()的toString();       //这是什么使[开始]我的应用程序崩溃
            SQLiteDbHelper DATABASE2 =新SQLiteDbHelper(本);
    SQLiteDatabase数据库= database2.getWritableDatabase();
       //这是什么让我的应用程序崩溃[完]    ContentValues​​值=新ContentValues​​();
    values​​.put(SQLiteDbHelper.COLUMN_NAME_SP,SP);
    values​​.put(SQLiteDbHelper.COLUMN_NAME_ST,ST);
    长insertId = database.insert(SQLiteDbHelper.TABLE_NAME,空,价值观);}


解决方案

您忘了把逗号(,)后,每列名在创建表查询更改为:

 私有静态最后弦乐DATABASE_CREATE =
        CREATE TABLE+ TABLE_NAME +
         (+
        COLUMN_NAME_SP +文本不为空,+
        COLUMN_NAME_ST +文本不为空+
        );;

I have been using this tutorial for some part, but I had to make some little changes and my app crashes on mobile phone. There are no errors in code, though!http://www.vogella.com/articles/AndroidSQLite/article.html

public class SQLiteDbHelper extends SQLiteOpenHelper {
    public static final String TABLE_NAME = "table";
    public static final String COLUMN_NAME_SP = "SP";
    public static final String COLUMN_NAME_ST = "ST";

    // If you change the database schema, you must increment the database version.
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "SPT.db";

    private static final String DATABASE_CREATE =
        "CREATE TABLE " + TABLE_NAME +
         " (" +
        COLUMN_NAME_SP + " text not null" +
        COLUMN_NAME_ST + " text not null" +
        " )";

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

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
      Log.w(SQLiteDbHelper.class.getName(),
          "Upgrading database from version " + oldVersion + " to "
              + newVersion + ", which will destroy all old data");
      database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    }

}

And then, later on, I have this code for a method for button click:

            public void Save(View view){
            String st = editTextSt.getText().toString();
            String sp = editTextSp.getText().toString();

       //this is what makes my app crash [beginning]
            SQLiteDbHelper database2 = new SQLiteDbHelper(this);
    SQLiteDatabase database = database2.getWritableDatabase();
       //this is what makes my app crash [end]

    ContentValues values = new ContentValues();
    values.put(SQLiteDbHelper.COLUMN_NAME_SP, sp);
    values.put(SQLiteDbHelper.COLUMN_NAME_ST, st);
    long insertId = database.insert(SQLiteDbHelper.TABLE_NAME, null, values);}
解决方案

you forget to place comma(,) after every column name in create table query change it as:

private static final String DATABASE_CREATE =
        "CREATE TABLE " + TABLE_NAME +
         " (" +
        COLUMN_NAME_SP + " text not null, " +
        COLUMN_NAME_ST + " text not null " +
        ");";

这篇关于Android应用程序崩溃的调用SQLiteHelper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 09:29