2016-05-07 110 views
-2

我是Android新手,我試圖通過函數putData()創建數據庫並在其中插入元素。
該應用運行時沒有任何錯誤,但沒有顯示任何元素,只是一個黑色的Activity。
我不知道發生了什麼錯誤,請幫忙顯示SQLite數據庫

DBHelper類

public class DBHelper extends SQLiteOpenHelper { 

    public static final String DATABASE_NAME = "food.db"; 
    public static final String TABLE_NAME = "food_calorie"; 
    public static final String COL_1 = "ID"; 
    public static final String COL_2 = "NAME"; 
    public static final String COL_3 = "CALORIE"; 

    public DBHelper(Context context) { 
     super(context, DATABASE_NAME, null, 1); 
     SQLiteDatabase db = this.getWritableDatabase(); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("create table " + TABLE_NAME +" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,CALORIE INTEGER)"); 
     putData(); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME); 
     onCreate(db); 
    } 

    public void putData(){ 
     insertData("Apple", 168); 
     insertData("Mango", 194); 
    } 

    public void insertData(String name, int calorie){ 
     SQLiteDatabase db=this.getWritableDatabase(); 
     ContentValues CV=new ContentValues(); 

     CV.put(COL_2,name); 
     CV.put(COL_3,calorie); 
     db.insert(TABLE_NAME,null,CV); 
     db.close(); 
    } 


    public String printDB(){ 
     String dbString=""; 
     SQLiteDatabase db=this.getWritableDatabase(); 
     String query="SELECT * FROM " + TABLE_NAME + " WHERE 1"; 

     Cursor c= db.rawQuery(query,null); 

     //Move to the first row in your result 
     c.moveToFirst(); 

     while(!c.isAfterLast()){ 
      if(c.getString(c.getColumnIndex("TABLE_NAME"))!=null){ 
       dbString+= c.getString(c.getColumnIndex("TABLE_NAME")); 
       dbString += "\n"; 
      } 
     } 
     db.close();; 
     return dbString; 
    } 
} 

MinActivity類

public class MainActivity extends AppCompatActivity { 

    DBHelper MyDb; 
    TextView showDBText; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     showDBText=(TextView) findViewById(R.id.showDB); 
     MyDb = new DBHelper(this); 
     printDatabase(); 
    } 
    // Buckys Video 54 
    public void printDatabase(){ 

     //printDB is a function in DBHelper 
     String dbString = MyDb.printDB(); 
     showDBText.setText(dbString); 

    } 
} 

XML文件

<ScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:text="Large Text" 
      android:id="@+id/showDB" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="4dp" /> 
</ScrollView> 
+0

能否請您解釋一下其中的含義是'... + 「WHERE 1」',在你的幻想? –

+0

其中1表示滿足所有條件。 我從newboston複製了方法 - 初學者的Android應用程序開發 - 53 - 打印數據庫結果 –

+0

請**刪除**那種荒謬,無用和潛在危險的情況!簡單地**沒有**任何WHERE條件,'滿足每個條件'。 –

回答

1

你的錯誤(除了可怕的WHERE 1條款)是:

if(c.getString(c.getColumnIndex("TABLE_NAME"))!=null){ 
       dbString+= c.getString(c.getColumnIndex("TABLE_NAME")); 

您必須指定一個列名,而不是表名

即:

if(c.getString(c.getColumnIndex("NAME"))!=null){ 
       dbString+= c.getString(c.getColumnIndex("NAME")); 
+0

我用「NAME」改變了它,但是在應用程序中再次沒有改變。 –

+0

如果你調試你的'printDB()',你應該在我修改後看到結果。或者由於某種原因,表格是空的。嘗試卸載您的應用程序並重新運行它。 –