【发布时间】:2015-07-05 11:02:06
【问题描述】:
我正在尝试创建一个 SQL 数据库来存储本地联系人信息(主要是电话号码(稍后验证)和显示名称,以及用户指定的布尔值。我创建了一个新的 contactdb 类,但是我在尝试使用 getcontext() 时遇到了一些问题。put 的次要值也无法解析。有人对如何解决这个问题有任何提示吗?我是否需要为 Dbhelper 创建一个新的类文件? 或者该位是否打算在 mainactivity 类中实例化,我希望在其中读取/写入信息到 DB?
package treehouse.greenlight;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.*;
import android.os.Bundle;
import android.content.ContentValues;
import android.view.View;
public final class contactdb {
// To prevent someone from accidentally instantiating the contract class,
// give it an empty constructor.
public contactdb() {
}
/* Inner class that defines the table contents */
public static abstract class dbEntry implements BaseColumns {
public static final String TABLE_NAME = "Contacts";
public static final String COLUMN_NAME_ENTRY_ID = "entryid";
public static final Boolean COLUMN_NAME_STATUS = false;
public static final String COLUMN_NAME_PHONE = "0000000000";
public static final String COLUMN_NAME_NAME = "N/A";
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + dbEntry.TABLE_NAME + " (" +
dbEntry._ID + " INTEGER PRIMARY KEY," +
dbEntry.COLUMN_NAME_STATUS + TEXT_TYPE + COMMA_SEP +
dbEntry.COLUMN_NAME_NAME + TEXT_TYPE + COMMA_SEP +
dbEntry.COLUMN_NAME_PHONE + TEXT_TYPE + COMMA_SEP +// Any other options for the CREATE command
" )";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + dbEntry.TABLE_NAME;
public class DbHelper extends SQLiteOpenHelper {
DbHelper mDbHelper = new DbHelper(getContext());
// 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 = "FeedReader.db";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
// Gets the data repository in write mode
db = mDbHelper.getWritableDatabase();
// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
values.put(dbEntry.COLUMN_NAME_ENTRY_ID, id);
values.put(dbEntry.COLUMN_NAME_STATUS, status);
values.put(dbEntry.COLUMN_NAME_PHONE, content);
// Insert the new row, returning the primary key value of the new row
long newRowId;
newRowId = db.insert(
dbEntry.TABLE_NAME,
dbEntry.COLUMN_NAME_NULLABLE,
values);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}
}
}
【问题讨论】:
标签: java android android-sqlite android-contacts