0

我不是 android 编程方面的专家,这是一个课程作业。每次我尝试使用模拟器插入数据时,logcat 都会告诉我该表不存在。我想我可能没有正确调用我的 onCreate 方法,但我不知道。想知道是否有人可以向我指出,这是我的代码:

package com.mknowles.locationapp;

import android.content.ContentValues;

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

public class Entries {

    public static final String KEY_ID = "_id";
    public static final String KEY_TITLE = "title";
    public static final String KEY_LOCATION = "location";
    public static final String KEY_DATE = "entryDate";
    public static final String KEY_TIME = "entryTime";
    public static final String KEY_DESCRIPTION = "description";

    private static final String DB_NAME = "LocationEntries_db";
    private static final String DB_TABLE = "entries_table";
    private static final int DB_VERSION = 1;

    private DbHelper dbhelper;
    private final Context appContext;
    private SQLiteDatabase entriesDb;

    private static class DbHelper extends SQLiteOpenHelper {

        public DbHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL(
                    "CREATE TABLE " + DB_TABLE + " ("
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
            + KEY_TITLE + " TEXT NOT NULL, "
            + KEY_LOCATION + "TEXT, "
            + KEY_DATE + " TEXT NOT NULL, "
            + KEY_TIME + " TEXT, "
            + KEY_DESCRIPTION + " TEXT);"
                );

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
            onCreate(db);
        }

    }

    public Entries(Context context){
        appContext = context;

    }

    public Entries open(){
        dbhelper = new DbHelper(appContext);
        entriesDb = dbhelper.getWritableDatabase();
        return this;
    }

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

    public long createEntry(String title, String location, String date, String time, String description) {
        // TODO Auto-generated method stub
        ContentValues cv = new ContentValues();
        cv.put(KEY_TITLE, title);
        cv.put(KEY_LOCATION, location);
        cv.put(KEY_DATE, date);
        cv.put(KEY_TIME, time);
        cv.put(KEY_DESCRIPTION, description);
        return entriesDb.insert(DB_TABLE, null, cv);
    }
}
package com.mknowles.locationapp;

import android.os.Bundle;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MakeEntryActivity extends Activity implements OnClickListener {

    Button sqlSave;
    EditText mTitle, mLocation, mDate, mTime, mDescription;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_make_entry);
        mTitle = (EditText) findViewById(R.id.etTitle);
        mLocation = (EditText) findViewById(R.id.etLocation);
        mDate = (EditText) findViewById(R.id.etDate);
        mTime = (EditText) findViewById(R.id.etTime);
        mDescription = (EditText) findViewById(R.id.etDescription);
        sqlSave = (Button) findViewById(R.id.bSave);

        sqlSave.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.bSave:

            String title = mTitle.getText().toString();
            String location = mLocation.getText().toString();
            String date = mDate.getText().toString();
            String time = mTime.getText().toString();
            String description = mDescription.getText().toString();

            Entries entry = new Entries(MakeEntryActivity.this);
            entry.open();
            entry.createEntry(title, location, date, time, description);
            entry.close();

            Toast t = Toast.makeText(this, "Saving", Toast.LENGTH_SHORT);
            t.show();

            break;
        }
    }
}
4

2 回答 2

2

您的创建中有一个小错字:

+ KEY_LOCATION + "TEXT, "

应该:

+ KEY_LOCATION + " TEXT, "
于 2013-04-16T18:29:27.387 回答
0

dbhelper = new DbHelper(appContext);从中删除public Entries open()

于 2013-04-16T18:32:09.717 回答