我的数据库的密码,我试图插入记录值,但我在创建记录和插入值时得到nullpointer异常,下面是我的Databasehelper代码
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String KEY_ROWID = "_id";
public static final String KEY_CODE = "code";
public static final String KEY_NAME = "name";
public static final String KEY_CONTINENT = "continent";
public static final String KEY_REGION = "region";
public static final String KEY_AREA = "area";
private static final String TAG = "CountriesDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
String password = "foo123";
private static final String DATABASE_NAME = "salesreports";
private static final String SQLITE_TABLE = "salesandreports";
private static final String SQLITE_TABLE1 = "salescharts";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"CREATE TABLE if not exists " + SQLITE_TABLE + " (" +
KEY_ROWID + " integer PRIMARY KEY autoincrement," +
KEY_CODE + " INTEGER," +
KEY_NAME + " INTEGER," +
KEY_CONTINENT + " INTEGER," +
KEY_REGION + " INTEGER," +
KEY_AREA + " INTEGER" +
")";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
onCreate(db);
}
public long createsalesCountry(String code, String name,
String continent, String region,String area) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_CODE, code);
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_CONTINENT, continent);
initialValues.put(KEY_REGION, region);
initialValues.put(KEY_AREA, area);
return mDb.insert(SQLITE_TABLE, null, initialValues);
}
public boolean deleteAllCountries() {
int doneDelete = 0;
doneDelete = mDb.delete(SQLITE_TABLE, null , null);
Log.w(TAG, Integer.toString(doneDelete));
return doneDelete > 0;
}
public Cursor fetchCountriesByName(String inputText) throws SQLException {
Log.w(TAG, inputText);
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION,KEY_AREA},
null, null, null, null, null,null);
}
else {
mCursor = mDb.query(true, SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION,KEY_AREA},
KEY_NAME + " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor fetchAllCountries() {
Cursor mCursor = mDb.query(SQLITE_TABLE, new String[] {KEY_ROWID,
KEY_CODE, KEY_NAME, KEY_CONTINENT, KEY_REGION,KEY_AREA},
null, null, null, null, null,null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public void insertsalesvalues() {
createCountry("1","1","1","1","1");}}
以下是我的活动
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutToDisplayChart=(LinearLayout)findViewById(R.id.relative_layout1);
SQLiteDatabase.loadLibs(this);
String password = "foo123";
dbHelper = new DatabaseHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase(password);
//dbHelper.insertsalesvalues();
dbHelper.insertSomeCountries();
Intent achartIntent = new aChart_Example().execute(MainActivity.this,LayoutToDisplayChart);
以下是我的追踪
Caused by: java.lang.NullPointerException
09-17 11:02:01.500: E/AndroidRuntime(27757): at com.example.mychartapp.DatabaseHelper.createsalesCountry(DatabaseHelper.java:113)
09-17 11:02:01.500: E/AndroidRuntime(27757): at com.example.mychartapp.DatabaseHelper.insertsalesvalues(DatabaseHelper.java:198)
09-17 11:02:01.500: E/AndroidRuntime(27757): at com.example.mychartapp.MainActivity.onCreate(MainActivity.java:26)
答案 0 :(得分:0)
添加此行解决了我的问题**SQLiteDatabase mDb = this.getWritableDatabase(password);**
public long createsalesCountry(String code, String name,
String continent, String region,String area) {
**SQLiteDatabase mDb = this.getWritableDatabase(password);**
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_CODE, code);
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_CONTINENT, continent);
initialValues.put(KEY_REGION, region);
initialValues.put(KEY_AREA, area);
return mDb.insert(SQLITE_TABLE, null, initialValues);
}
public void insertSalesvalues() {
**SQLiteDatabase mDb = this.getWritableDatabase(password);**
createCountry("1","1","1","1","1");
}
答案 1 :(得分:0)
您要做的第一件事是,检查您的数据库文件的位置,资产,/ data / data /您的软件包名称/ your_application_name。如果它在您的资产中,则必须将其复制到/ data / data /您的包名/文件夹中。然后,在复制DB文件后,将该DB文件保存到PC,检查数据库文件的结构,其中存在的数据(如果有)。您是在说,您无法插入或检索任何数据吗?通过以这种方式检查数据库文件,您可以了解后端的错误。但是,据我所知,我的代码中没有发现任何错误。