按下按钮时应用程序不断崩溃

时间:2015-01-13 02:33:14

标签: java android eclipse macos sqlite

每次尝试运行我的应用时,按下按钮时都会崩溃。 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);
    }
}

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

引起:android.database.sqlite.SQLiteException:near" Day&#34 ;:语法错误(代码1):,编译时:SELECT DISTINCT _id,驱动时间,时间从mainTable WHERE _id = -1

3 个答案:

答案 0 :(得分:2)

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

将报价放在'开车时间'和'时间'或将列重命名为Drive_TimeTime_Of_Day。这不是写的有效SQL。

答案 1 :(得分:0)

双引号适用于列名称,请将其视为

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

答案 2 :(得分:0)

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

将您的代码更改为:

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

希望它有效。