【问题标题】:App keeps crashing when a button is pressed按下按钮时应用程序不断崩溃
【发布时间】:2015-03-10 22:24:32
【问题描述】:

每次我尝试运行我的应用程序时,它都会在我按下按钮时崩溃。 LogCat 说原因
在这个文件中。这是 LogCat 消息和导致崩溃的文件 据说。感谢您的所有帮助。

这是错误所在的文件:

package com.example.drive.drivercorder;

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



public class dbadapter {

private static final String TAG = "DBAdapter";

public static final String KEY_ROWID = "_id";
public static final int COL_ROWID = 0;

public static final String KEY_DRIVETIME = "Drive Time";
public static final String KEY_NIGHTORDAY = "Time of Day";



public static final int COL_DRIVETIME = 1;
public static final int COL_NIGHTORDAY = 2;



public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_DRIVETIME,    
KEY_NIGHTORDAY, };


public static final String DATABASE_NAME = "MyDb";
public static final String DATABASE_TABLE = "mainTable";

public static final int DATABASE_VERSION = 2;   

private static final String DATABASE_CREATE_SQL = 
        "create table " + DATABASE_TABLE 
        + " (" + KEY_ROWID + " integer primary key autoincrement, "

        + KEY_DRIVETIME + " integer not null, "
        + KEY_NIGHTORDAY + " text not null "



        + ");";


private DatabaseHelper myDBHelper;
private SQLiteDatabase db;



public dbadapter(Context ctx) {
    myDBHelper = new DatabaseHelper(ctx);
}


public dbadapter open() {
    db = myDBHelper.getWritableDatabase();
    return this;
}


public void close() {
    myDBHelper.close();
}


public long insertRow(int drivetime, String nightorday) {

    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_DRIVETIME, drivetime);
    initialValues.put(KEY_NIGHTORDAY, nightorday);



    return db.insert(DATABASE_TABLE, null, initialValues);
}




public Cursor getAllRows() {
    String where = null;
    Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                        where, null, null, null, null, null);
    if (c != null) c.moveToFirst();
    return c;
}


public Cursor getRow(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                    where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}


public boolean updateRow(long rowId, int drivetime, String nightorday) {
    String where = KEY_ROWID + "=" + rowId;

    ContentValues newValues = new ContentValues();
    newValues.put(KEY_DRIVETIME, drivetime);
    newValues.put(KEY_NIGHTORDAY, nightorday);

    return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}


private static class DatabaseHelper extends SQLiteOpenHelper{
    DatabaseHelper(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase _db) {
        _db.execSQL(DATABASE_CREATE_SQL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading application's database from version " + oldVersion
                + " to " + newVersion + ", which will destroy all old data!");

        _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);


        onCreate(_db);
    }
}

} 以下是指示崩溃原因的日志 cat 消息。

原因:android.database.sqlite.SQLiteException: near "Day": syntax error (code 1): , while compile: SELECT DISTINCT _id, Drive Time, Time of Day FROM mainTable WHERE _id=-1

【问题讨论】:

  • logcat 消息在哪里?它指的是文件中的哪一行?
  • 老实说,我不确定我是否是 android 编程新手。
  • 据我所知,它没有说在哪里。

标签: java android eclipse macos sqlite


【解决方案1】:
SELECT DISTINCT _id, Drive Time, Time of Day FROM mainTable WHERE _id=-1

在“行驶时间”和“时间”周围加上引号,或将列重命名为 Drive_TimeTime_Of_Day。这不是所写的有效 SQL。

【讨论】:

    【解决方案2】:

    双引号适用于列名,试试吧

    SELECT DISTINCT _id, "Drive Time", "Time of Day" FROM mainTable WHERE _id=-1
    

    【讨论】:

      【解决方案3】:

      您需要在描述列名时加上双引号。

      将您的代码更改为:

      SELECT DISTINCT _id, "Drive Time", "Time of Day" FROM mainTable WHERE _id=-1
      

      希望它有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-05
        • 2018-07-06
        • 1970-01-01
        相关资源
        最近更新 更多