由于sqlite的问题导致android应用程序崩溃

时间:2013-04-19 13:10:13

标签: android database sqlite crash

我正在尝试编写一个应用程序,我想将sql用于我的数据库。几乎每个查询中的命令都有问题。 这是我的DBAdapter.java文件:

public class DBAdapter {

static final String KEY_ROWID = "_id";
static final String KEY_HOUR_BEGIN = "hour_begin";
static final String KEY_HOUR_END = "hour_end";
static final String KEY_TEM_NAME = "tem_name";
static final String KEY_PERCENT = "percent";
static final String TAG = "DBAdapter";

static final String DATABASE_NAME = "MYDB";
static final String DATABASE_TABLE_TEMPLATE = "template";
static final int DATABASE_VERSION = 2;

static final String DATABASE_CREATE_TEMPLATE = 
        "create table template (_id integer primary key autoincrement, " +
        "tem_name text not null, hour_begin text not null, hour_end text not null, " +
        "percent text not null";


final Context context;

DatabaseHelper DBHelper;
SQLiteDatabase db;

public DBAdapter(Context ctx)
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper
{
    DatabaseHelper(Context context)
    {
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        try{
            db.execSQL(DATABASE_CREATE_TEMPLATE);
        }catch(SQLException e){
            e.printStackTrace();
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Updating database from version " + oldVersion + " to "
    + newVersion + ", which will all old data");
        db.execSQL("DROP TABLE IF EXISTS template");
        onCreate(db);

    }
}

//opens the database
public DBAdapter open() throws SQLException
{
    db = DBHelper.getWritableDatabase();
    return this;
}

//closes the database
public void close()
{
    DBHelper.close();
}

            //insert template into the database
            public long insertTemplate(String tem_name, String hour_begin, String hour_end, String percent)
            {
                ContentValues initialValues = new ContentValues();
                initialValues.put(KEY_TEM_NAME, tem_name);
                initialValues.put(KEY_HOUR_BEGIN, hour_begin);
                initialValues.put(KEY_HOUR_END, hour_end);
                initialValues.put(KEY_PERCENT, percent);
                return db.insert(DATABASE_TABLE_TEMPLATE, null, initialValues);
            }

            //deletes a particular template
            public boolean deleteTemplate(long rowId)
            {
                return db.delete(DATABASE_TABLE_TEMPLATE, KEY_ROWID + "=" + rowId, null) > 0;
            }

            //retrieves all the template
            public Cursor getAllTemplates()
            {
                return db.query(DATABASE_TABLE_TEMPLATE, new String[] 
                        {KEY_ROWID, KEY_TEM_NAME, KEY_HOUR_BEGIN, KEY_HOUR_END, 
                        KEY_PERCENT}, null, null, null, null, null);
            }

            //retrieves a particular template
            public Cursor getTemplate(long rowId) throws SQLException
            {
                Cursor mCursor = 
                        db.query(true, DATABASE_TABLE_TEMPLATE, new String[]
                                {KEY_ROWID, KEY_TEM_NAME, KEY_HOUR_BEGIN, KEY_HOUR_END, 
                                KEY_PERCENT}, KEY_ROWID + "=" + rowId, 
                                null, null, null, null, null);
                if(mCursor != null)
                {
                    mCursor.moveToFirst();
                }
                return mCursor;
            }

            //updates a template
            public boolean updateTemplate(long rowId, String tem_name, String hour_begin, String hour_end, String percent)
            {
                ContentValues args = new ContentValues();
                args.put(KEY_TEM_NAME, tem_name);
                args.put(KEY_HOUR_BEGIN, hour_begin);
                args.put(KEY_HOUR_END, hour_end);
                args.put(KEY_PERCENT, percent);
                return db.update(DATABASE_TABLE_TEMPLATE, args, KEY_ROWID + "=" + rowId, null) > 0;
            }

}

这是我的mainActivity.java 这是onCreate函数:

DBAdapter db = new DBAdapter(this);
    db.open();

    //inserting template
    long id = db.insertTemplate("morning", "06:00", "14:00", "125");

    //getting all the templates
    Cursor c = db.getAllTemplates();
    if(c.moveToFirst())
    {
        do{
            DisplayTemplate(c);
        }while(c.moveToNext());
    }

    //getting one template
    Cursor c = db.getTemplate(1);
    if(c.moveToFirst())
        DisplayTemplate(c);
    else
        Toast.makeText(this, "template not found!", Toast.LENGTH_LONG).show();

    //updating one template
    if(db.updateTemplate(1, "fgdhjf", "12:30:", "20:32", "200"))
        Toast.makeText(this, "update successful", Toast.LENGTH_LONG).show();
    else
        Toast.makeText(this, "update unsuccessful", Toast.LENGTH_LONG).show();

    //deleting one template
    if(db.deleteTemplate(5))
        Toast.makeText(this, "delete successful", Toast.LENGTH_LONG).show();
    else
        Toast.makeText(this, "delete unsuccessful", Toast.LENGTH_LONG).show();

    db.close();

这是onCreate函数中的DisplayTemplate函数:

private void DisplayTemplate(Cursor c) {
    Toast.makeText(this, "template_id: " + 
c.getString(0) + "\n" + "name: " + 
            c.getString(1) + "\n" + 
"hour begin: " + c.getString(2) + "\n" + 
            "hour end: " + c.getString(3) + 
            "\n" + "percent: " + c.getString(4), 
            Toast.LENGTH_LONG).show();

我对mainActivity中的每个函数都有问题。我检查了调试器,问题出在DBAdapter文件中。请有人告诉我,我做错了什么? 非常感谢!

1 个答案:

答案 0 :(得分:0)

这是语法的一个小问题。就这样。谢谢你的时间