显示SQLite数据库

时间:2016-05-07 17:16:34

标签: android sqlite android-sqlite

我是Android开发的新手,我正在尝试通过函数putData()创建数据库并在其中插入元素。
该应用程序正在运行,没有任何错误,但它没有显示任何元素,只是一个黑色的活动 我不知道出了什么问题,请帮忙

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 Class

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>

1 个答案:

答案 0 :(得分: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"));