Android:Select语句在数据库中返回null

时间:2011-11-08 06:07:21

标签: android database

我以json数组的形式从服务器端获得响应。我需要首先解析它们然后将这些值插入到database.I已经解析了数据。现在我需要将该数据插入数据库。我使用以下类在数据库中创建dinserting数据。:

以下是我的帮助班:

public class WineDatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "applicationdata";
    private static String DB_PATH = "/data/data/com.emxWinwcountry./databases/";
    private static final int DATABASE_VERSION = 1;

    public static final String DATABASE_CREATE ="CREATE TABLE resources(KEY_TYPE text  , KEY_ENCODING text , KEY_WIDTH text, KEY_HEIGHT text, KEY_DATA text ,KeyIId text)";
    public static final String DATABASE_NAVIGATION_CREATE = "create table navigation (KEY text,TYPE1 text ,LINK_NAME text,DISPLAY_NAME text ,FORWARD_ICON text ,HIGHLIGHTED_ICON_WITH_TEXT text,BANNER_IMAGE text,CHILDREN text,ICON_WITHOUT_TEXT text, HIGHLIGHTED_ICON_WITHOUT_TEXT text,ICON_WITH_TEXT text)";



    public WineDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }



    // Method is called during creation of the database



    @Override
    public void onCreate(SQLiteDatabase database) {
        try{
            database.execSQL(DATABASE_CREATE);
        database.execSQL(DATABASE_NAVIGATION_CREATE);

    }
    catch (Exception e) {
        System.out.println("in on creste db"+e);
    }}

    // Method is called during an upgrade of the database, e.g. if you increase
    // the database version
    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion,
            int newVersion) {
        Log.w(WineDatabaseHelper.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        database.execSQL("DROP TABLE IF EXISTS todo");
        onCreate(database);
    }
}

以下是我的适配器类

       public class WineDatabaseAdapter {

    // Database fields
    public static final String KEY_TYPE = "type";
    public static final String KEY_ID = "_id";
    public static final String KEY_WIDTH = "width";
    public static final String KEY_HEIGHT = "height";
    public static final String KEY_ENCODING = "encoding";
    public static final String KEY_DATA = "data";
    private static final String KeyIId = "iid";
    private static  String DATABASE_TABLE = "resources";
    private static  String DATABASE_TABLE_NAVIGATION = "navigation";
    private Context context;
    private SQLiteDatabase database;
    private WineDatabaseHelper dbHelper;

    public static final String DISPLAY_NAME= "display_name";
    public static final String KEY="key";
    public static final String TYPE1 = "type1";
    public static final String LINK_NAME = "link_name";
    private static final String FORWARD_ICON = "forward_icon";
    private static final String HIGHLIGHTED_ICON_WITH_TEXT = "highlighted_icon_with_text";
    private static final String ICON_WITHOUT_TEXT = "icon_without_text";
    private static final String HIGHLIGHTED_ICON_WITHOUT_TEXT = "highlighted_icon_without_text";
    private static final String Banner_Image = "banner_image";
    private static final String CHILDREN= "children";
    private static final String ICON_WITH_TEXT = "icon_with_text";
      private SQLiteDatabase db;
      private SQLiteStatement insertStmt;


       private static final String INSERT = "insert into " 
         + DATABASE_TABLE + "(KEY_TYPE,KEY_ENCODING,KEY_WIDTH,KEY_HEIGHT,KEY_DATA,KeyIId)"+" values (?,?,?,?,?,?)";


       private static final String INSERT_NAVIGATION = "insert into " 
             + DATABASE_TABLE_NAVIGATION + "(KEY,TYPE1,LINK_NAME,DISPLAY_NAME,FORWARD_ICON,HIGHLIGHTED_ICON_WITH_TEXT,ICON_WITHOUT_TEXT,HIGHLIGHTED_ICON_WITHOUT_TEXT,Banner_Image,CHILDREN,ICON_WITH_TEXT)"+" values (?,?,?,?,?,?,?,?,?,?,?)";


       public WineDatabaseAdapter(Context context) {
         try{
           this.context = context;
       WineDatabaseHelper openHelper = new WineDatabaseHelper(context);
       this.db=openHelper.getWritableDatabase();
      this.insertStmt=this.db.compileStatement(INSERT);
      this.insertStmt=this.db.compileStatement(INSERT_NAVIGATION);
       }
       catch(Exception e)
       {
           System.out.println(e);
       }
       }

       public long insert(String KEY_TYPE ,String KEY_ENCODING,String KEY_WIDTH,String KEY_HEIGHT,String KEY_DATA,String KeyIId){
           this.insertStmt.bindString(1, KEY_TYPE);
           this.insertStmt.bindString(2, KEY_ENCODING);
           this.insertStmt.bindString(3, KEY_WIDTH);
           this.insertStmt.bindString(4, KEY_WIDTH);
           this.insertStmt.bindString(5, KEY_DATA);
           this.insertStmt.bindString(6, KeyIId);
           return this.insertStmt.executeInsert();
       }

       public long insertNavigation(String KEY ,String TYPE1,String LINK_NAME,String DISPLAY_NAME,String FORWARD_ICON,String HIGHLIGHTED_ICON_WITH_TEXT,String ICON_WITHOUT_TEXT,String HIGHLIGHTED_ICON_WITHOUT_TEXT,String Banner_Image,String CHILDREN,String ICON_WITH_TEXT ){
           this.insertStmt.bindString(1, KEY);
           this.insertStmt.bindString(2, TYPE1);
           this.insertStmt.bindString(3, LINK_NAME);
           this.insertStmt.bindString(4, DISPLAY_NAME);
           this.insertStmt.bindString(5, FORWARD_ICON);
           this.insertStmt.bindString(6, HIGHLIGHTED_ICON_WITH_TEXT);
           this.insertStmt.bindString(7, ICON_WITHOUT_TEXT);
           this.insertStmt.bindString(8, HIGHLIGHTED_ICON_WITHOUT_TEXT);
           this.insertStmt.bindString(9, Banner_Image);
           this.insertStmt.bindString(10, CHILDREN);
           this.insertStmt.bindString(11, ICON_WITH_TEXT);

           return this.insertStmt.executeInsert();
       }

       public Cursor fetchAllResources() {
            return database.query(DATABASE_TABLE, new String[] { KEY_TYPE,
                    KEY_ENCODING, KEY_WIDTH, KEY_WIDTH,KEY_DATA, KeyIId}, null, null, null,
                    null, null);
        }

       public List<String> selectTable(){
           List<String> list=new ArrayList<String>();
       Cursor c = db.rawQuery("SELECT * FROM " +
               DATABASE_TABLE , null);
       if(c.moveToFirst()){
           do{
               list.add(c.getString(0));
           }
           while(c.moveToNext());
       }
       if(c !=null && !c.isClosed()){
           c.close();
       }
       return list;
       }
       public void deleteAll(){
           this.db.delete(DATABASE_TABLE, null, null);
       }


       public void deleteAllNavigation(){
           this.db.delete(DATABASE_TABLE_NAVIGATION, null, null);
       }

       public List<String> selectAll(){
           List<String> list=new ArrayList<String>();
           Cursor cursor=this.db.query(DATABASE_TABLE, new String[]{"KEY_TYPE","KEY_ENCODING","KEY_WIDTH","KEY_HEIGHT","KEY_DATA","KeyIId"},
                   null, null, null, null, "KeyIId");

           if(cursor.moveToFirst()){
               do{
                   list.add(cursor.getString(0));
               }
               while(cursor.moveToNext());
           }
           if(cursor !=null && !cursor.isClosed()){
               cursor.close();
           }
           return list;
       }


       public List<String> selectAllNavigation(){
           List<String> list=new ArrayList<String>();
           Cursor cursor=this.db.query(DATABASE_TABLE_NAVIGATION, new String[]{"KEY,TYPE1","LINK_NAME","DISPLAY_NAME","FORWARD_ICON","HIGHLIGHTED_ICON_WITH_TEXT","ICON_WITHOUT_TEXT","HIGHLIGHTED_ICON_WITHOUT_TEXT","Banner_Image","CHILDREN,ICON_WITH_TEXT"},
                   null, null, null, null, "Key");

           if(cursor.moveToFirst()){
               do{
                   list.add(cursor.getString(0));
               }
               while(cursor.moveToNext());
           }
           if(cursor !=null && !cursor.isClosed()){
               cursor.close();
           }
           return list;
       }

    public WineDatabaseAdapter open() throws SQLException {
        dbHelper = new WineDatabaseHelper(context);
        database = dbHelper.getWritableDatabase();
        return this;
    }

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

    public long  createNavigation(String key, String type, String link_name, String display_name,String forward_icon, String highlighted_icon_with_text, String banner_image,String highlighted_icon_without_text,String icon_without_text,String children,String icon_with_text ) { 

        ContentValues initialValues = createNavigationValues(key,type,link_name,display_name, forward_icon,
                highlighted_icon_with_text, banner_image, highlighted_icon_without_text,banner_image,highlighted_icon_without_text,icon_without_text);

        return database.insert(DATABASE_TABLE, null, initialValues);
    }

    public boolean updateNavigation (long rowId,String key,String type, String link_name,String display_name, String forward_icon, String highlighted_icon_with_text, String banner_image,String highlighted_icon_without_text,String icon_without_text,String children,String icon_with_text )
    {   ContentValues updateValues = createNavigationValues(key, type,link_name,display_name, forward_icon,
            highlighted_icon_with_text, banner_image, highlighted_icon_without_text,banner_image,highlighted_icon_without_text,icon_without_text);


        return database.update(DATABASE_TABLE, updateValues, KEY_ID + "="
                + rowId, null) > 0;
    }

    public boolean deleteNavigation(long rowId) {
        return database.delete(DATABASE_TABLE, KEY_ID + "=" + rowId, null) > 0;
    }

    public Cursor fetchAllNavigations() {
        return database.query(DATABASE_TABLE_NAVIGATION, new String[] { KEY_ID,TYPE1,FORWARD_ICON,LINK_NAME,HIGHLIGHTED_ICON_WITH_TEXT,Banner_Image,CHILDREN,ICON_WITHOUT_TEXT,HIGHLIGHTED_ICON_WITHOUT_TEXT,ICON_WITH_TEXT}, null, null, null,
                null, null);
    }

    public Cursor fetchNavigation(long rowId) throws SQLException {
        Cursor mCursor = database.query(true, DATABASE_TABLE_NAVIGATION, new String[] { KEY_ID,TYPE1,FORWARD_ICON,LINK_NAME,HIGHLIGHTED_ICON_WITH_TEXT,Banner_Image,CHILDREN,ICON_WITHOUT_TEXT,HIGHLIGHTED_ICON_WITHOUT_TEXT,ICON_WITH_TEXT},
                KEY_ID + "=" + rowId, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    public long  createTodo(String iid,String type, String encoding, String data) 
    {
        ContentValues initialValues = createContentValues(iid,type, 
                 encoding, data);

        return database.insert(DATABASE_TABLE, null, initialValues);
    }

    public boolean updateTodo(long rowId,String iid, String type, 
             String encoding, String data ) {
        ContentValues updateValues = createContentValues(iid, type,
                 encoding, data);

        return database.update(DATABASE_TABLE, updateValues, KEY_ID + "="
                + rowId, null) > 0;
    }

    public boolean deleteTodo(long rowId) {
        return database.delete(DATABASE_TABLE, KEY_ID + "=" + rowId, null) > 0;
    }

    public Cursor fetchAllTodos() {
        return database.query(DATABASE_TABLE, new String[] { KEY_ID,KeyIId,
                KEY_TYPE, KEY_WIDTH,KEY_HEIGHT, KEY_ENCODING, KEY_DATA }, null, null, null,
                null, null);
    }

    public Cursor fetchTodo(long rowId) throws SQLException {
        Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
                KEY_ID,KeyIId, KEY_TYPE, KEY_WIDTH, KEY_HEIGHT, KEY_ENCODING, KEY_DATA },
                KEY_ID + "=" + rowId, null, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    private ContentValues createContentValues( String iid,String type, 
             String encoding, String data) {
        ContentValues values = new ContentValues();

        values.put(KeyIId, iid);
        values.put(KEY_TYPE, type);
        values.put(KEY_ENCODING, encoding);
        values.put(KEY_DATA, data);
        return values;
    }

    private ContentValues createNavigationValues(String key,String type, String link_name,String display_name, String forward_icon, String highlighted_icon_with_text, String banner_image,String highlighted_icon_without_text,String icon_without_text,String children,String icon_with_text ) {
        ContentValues values = new ContentValues();
        values.put(KEY, key);
        values.put(TYPE1, type);
        values.put(LINK_NAME, link_name);
        values.put(DISPLAY_NAME, display_name);
        values.put(FORWARD_ICON, forward_icon);
        values.put(HIGHLIGHTED_ICON_WITH_TEXT, highlighted_icon_with_text);
        values.put(Banner_Image, banner_image);
        values.put(CHILDREN, children);
        values.put(ICON_WITHOUT_TEXT, icon_without_text);
        values.put(HIGHLIGHTED_ICON_WITHOUT_TEXT, highlighted_icon_without_text);
        values.put(ICON_WITH_TEXT, icon_with_text);
        return values;
    }
}    

我将这些值插入到以下活动中:

     for (int ii = 0; ii < resources.length(); ii++) 
                                            {
                                                type = resources.getJSONObject(ii).getString("type").toString();
                                                encoding = resources.getJSONObject(ii).getString("encoding").toString();
                                                data1 = resources.getJSONObject(ii).getString("data").toString();
                                                iid = resources.getJSONObject(ii).getString("id").toString();
                                                try{
                                                width=resources.getJSONObject(ii).getString("width").toString();
                                                height=resources.getJSONObject(ii).getString("height").toString();
                                                }
                                                catch(Exception e){
                                                    e.printStackTrace();
                                                }
                                                db = new WineDatabaseAdapter(HomePageWithPhoneIsOfflineDialog.this);
    db.insert(type,encoding,width,height, data1,iid);
   db.fetchAllResources()

它给了我 null pointer exception on fetchAllResources() method 。相反,当我使用字符串构建器打印值时,它会打印值。

编辑:

它返回一个空数组,即没有像[]那样的任何值。是不是在数据库中插入了值?然后如何在数据库中插入值。

任何人都可以告诉我为什么会这样。      感谢

3 个答案:

答案 0 :(得分:3)

public Cursor fetchAllResources() {
    return database.query(DATABASE_TABLE, new String[] { KEY_TYPE,
        KEY_ENCODING, KEY_WIDTH, KEY_WIDTH,KEY_DATA, KeyIId}, null, null, null,
        null, null);
}

此处您正在使用永远不会初始化的database =&gt;空指针异常

答案 1 :(得分:1)

您不能在适配器类中使用SQLitedatabase类的数据库对象,因为该对象将为null,因为它未初始化。

而是将fetchAllResources()方法放在databasehelper类中,或者在使用数据库对象之前调用open()方法。

希望它有所帮助。

答案 2 :(得分:0)

因为在创建表时,您使用了KEY_TYPE,KEY_ID等作为字段名称,这些名称按原样使用。它们不被视为变量。因此您应该将表声明为:

public static final String DATABASE_CREATE ="CREATE TABLE resources(type text,encoding text,width text,height text,data text ,iid text)";

public static final String DATABASE_NAVIGATION_CREATE = "create table navigation (key text,type1 text ,link_name text,display_name text ,forward_icon text ,highlighted_icon_with_text text,banner_image text,childer text,icon_without_text text, highlighted_icon_without_text text,icon_with_text text)";

public static final String DATABASE_CREATE ="CREATE TABLE resources("+WineDatabaseAdapter.KEY_TYPE+" text,"+WineDatabaseAdapter.KEY_ENCODING+" text ,"+WineDatabaseAdapter.KEY_WIDTH+" text,"+WineDatabaseAdapter.KEY_HEIGHT+" text,"+WineDatabaseAdapter.KEY_DATA+" text,"+WineDatabaseAdapter.KeyIId+" text)";

public static final String DATABASE_NAVIGATION_CREATE = "create table navigation ("+WineDatabaseAdapter.KEY+" text,"+WineDatabaseAdapter.TYPE1+" text,"+WineDatabaseAdapter.LINK_NAME+" text,"+WineDatabaseAdapter.DISPLAY_NAME+" text ,"+WineDatabaseAdapter.FORWARD_ICON+" text ,"+WineDatabaseAdapter.HIGHLIGHTED_ICON_WITH_TEXT+" text,"+WineDatabaseAdapter.BANNER_IMAGE+" text,"+WineDatabaseAdapter.CHILDREN+" text,"+WineDatabaseAdapter.ICON_WITHOUT_TEXT+" text,"+WineDatabaseAdapter.HIGHLIGHTED_ICON_WITHOUT_TEXT+" text,"+WineDatabaseAdapter.ICON_WITH_TEXT+" text)";

修改:

还要将此方法添加到WineDatabaseAdapter:

public WineDatabaseAdpater open() throws SQLException
{
    dbcreator=new DatabaseCreator(context);
    sqdb=dbcreator.getWritableDatabase();
    return this;
}
public void close()
{
    dbcreator.close();
}

使用此课程时,请使用

WineDatabaseAdapter wda=new WineDatabaseAdapter(context);
wda.open();

wda.fetchAllResources();

wda.close();