简体   繁体   English

sqlite数据库读取错误

[英]sqlite database reading error

hi i am reading data from my database which is created using sqlite manager and then copied into data folder of my application but the cursor is returning zero when i tried to print the getcount what is wrong in my code below is my code this is the error 嗨,我从使用sqlite管理器创建的数据库中读取数据,然后将其复制到应用程序的数据文件夹中,但是当我尝试打印getcount时光标返回零,这是我的代码错误,这是我的代码

unable to start activity 无法开始活动

ComponentInfo{your.pckage.namespac/your.pckage.namespac.TabarActivity},second error ComponentInfo {your.pckage.namespac / your.pckage.namespac.TabarActivity},第二个错误

Caused by: java.lang.NullPointerException 造成原因: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();
        }


}

does these code print any message?(In your getContact method),is mCursor null? 这些代码会打印任何消息吗?(在您的getContact方法中),mCursor是否为null?

if (mCursor != null) {
    System.out.println(1);
    //Boolean t=mCursor.moveToFirst();
    Boolean t=mCursor.moveToNext();
    System.out.println(t);
}

EDIT:try this? 编辑:尝试这个吗? load image info from system ContentProvider 从系统ContentProvider加载图像信息

private final Uri PHOTO_URI = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
private final String[] PHOTO_PJT = new String[] {
        MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA,
        MediaStore.Images.Media.DATE_TAKEN, MediaStore.Images.Media.SIZE,
        MediaStore.Images.Media.DESCRIPTION, MediaStore.Images.Media.TITLE,
        MediaStore.Images.Media.MIME_TYPE };
ContentResolver mResolver = mContext.getContentResolver();
Cursor c = mResolver.query(PHOTO_URI, PHOTO_PJT, null, null, TITLESEARC);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM