0
您好我正在從我的數據庫中讀取數據,它是使用sqlite管理器創建的,然後複製到我的應用程序的數據文件夾中,但當我嘗試打印getcount時,遊標返回零,我的代碼中出現了什麼錯誤是我的代碼,這是錯誤sqlite數據庫讀取錯誤
無法啓動活動
ComponentInfo {your.pckage.namespac/your.pckage.namespac.TabarActivity},第二個錯誤
引起:顯示java.lang.NullPointerException
public class DBHelper {
public static final String KEY_ROWID = "id";
public static final String KEY_NAME = "name";
public static final String SMALL = "small";
public static final String LARGE = "large";
public static final String TYPE1 = "type1";
public static final String TYPE2 = "type2";
public static final String TYPE3 = "type3";
public static final String TEX = "tex";
public static final String ORIGIN = "origin";
public static final String DESCRIPTION = "description";
public static final String BOOKMARK = "bookmark";
public static final String KEY_EMAIL = "email";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "vegetable.db";
private static final String DATABASE_TABLE = "mac";
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_CREATE ="create table mac (id integer primary key autoincrement not null ,"+
"name VARCHAR, small integer,"+
"large integer, type1 integer,"+
"type2 integer, type3 integer,"+
"tex varchar, origin varchar,"+
"description varchar, bookmark varchar);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBHelper(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);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
}
//---opens the database---
public DBHelper open() throws SQLException
{
//db = DBHelper.getWritableDatabase();
db=DBHelper.getReadableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---deletes a particular contact---
public boolean deleteContact(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
//---retrieves all the contacts---
public Cursor getAllContacts()
{
return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
SMALL,LARGE,TYPE1,TYPE2,TYPE3,TEX,ORIGIN,DESCRIPTION,BOOKMARK}, null, null, null, null, null);
}
//---retrieves a particular contact---
public Cursor getContact(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
SMALL,LARGE,TYPE1,TYPE2,TYPE3,
TEXTURE,ORIGIN,DESCRIPTION,BOOKMARK}, KEY_ROWID + "=" + rowId, null,
null, null, null, null);
System.out.println(mCursor);
if (mCursor != null) {
System.out.println(1);
//Boolean t=mCursor.moveToFirst();
Boolean t=mCursor.moveToNext();
System.out.println(t);
}
return mCursor;
}
//---updates a contact---
public boolean updateContact(long rowId, String secondview)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME, secondview);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
public class MyListActivity extends ActivityGroup {
/** Called when the activity is first created. */
String tag = "Events";
String[] presidents;
String[] ima;
int i=0;
Intent intent;
Integer[] imageIDs= {R.drawable.abbaysmall,R.drawable.edalesmall};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listtype);
try {
String destPath = "/data/data/" + getPackageName() + "/databases/vegetable.db";
File f = new File(destPath);
if (!f.exists()) {
CopyDB(getBaseContext().getAssets().open("vegetable.db"),
new FileOutputStream(destPath));
System.out.println("hi");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
DBHelper db = new DBHelper(this);
//---get a contact---
db.open();
Cursor c = db.getContact(1);
System.out.println(c.getCount());
if (c.moveToFirst()){
}else
{
Toast.makeText(this, "No contact found", Toast.LENGTH_LONG).show();
db.close();
}
list.setAdapter(adapter);
//setListAdapter(adapter);
list.setOnItemClickListener(onListClick);
}
public void CopyDB(InputStream inputStream, OutputStream outputStream)
throws IOException {
//---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
}
是的,它打印錯誤 – Ghouse
對不起,我昨天回家了。 Sqlite的ID從0開始,也許沒有ID = 1的數據,哪一行會拋出NullPointException ... getCount()或其他地方? – JohnCookie
JohnCookie感謝重播代碼現在正在工作我已經忘記在查詢中包含一個列名稱。但是如果我有另一個問題,我該如何檢索放置在應用程序的數據/數據文件夾中的圖像 – Ghouse