数据输入到SQLite数据库表中没有显示出来

时间:2013-11-27 10:00:59

标签: android sqlite android-tablelayout

我正在尝试设置一个SQLite数据库,其中包含有关食物及其热值的一些信息。但是,当我键入信息,然后单击按钮查看时,该表只显示任何内容。我刚刚输入的信息没有显示出来。 我无法看到问题所在。需要帮助查看代码,让我知道那里有什么问题。

创建数据库:

public class FormDatabase 
{
public static final String KEY_ROWID = "_id";
public static final String KEY_FOOD = "food name";
public static final String KEY_CALORIE = "food_calories";

private static final String DATABASE_NAME = "Calories";
private static final String DATABASE_TABLE = "FoodTable";
private static final int DATABASE_VERSION = 1; 

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper
{

    public DbHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + 
        KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
        KEY_FOOD + " TEXT NOT NULL, " + 
        KEY_CALORIE + " TEXT NOT NULL);"        
        );
    }

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
        onCreate(db);
    }
}

public FormDatabase(Context c){
        ourContext = c;
    }

public FormDatabase open() throws SQLException {
        ourHelper = new DbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;
    }

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

public long createEntry(String food, String calorie) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_FOOD, food);
    cv.put(KEY_CALORIE, calorie);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);
}

public String getData() {
    // TODO Auto-generated method stub
    String [] columns = new String[]{ KEY_ROWID, KEY_FOOD, KEY_CALORIE};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null, null);
    String result = "";

    int iRow = c.getColumnIndex(KEY_ROWID);
    int iFood = c.getColumnIndex(KEY_FOOD);
    int iCalorie = c.getColumnIndex(KEY_CALORIE);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        result = result  + c.getString(iRow) + " " + c.getString(iFood) + " " + c.getString(iCalorie) + "\n";
    }
    return result;
}
}

更新并查看数据库:

public class DatabaseMain extends Activity implements OnClickListener{

Button sqlUpdate, sqlView;
EditText sqlFood, sqlCalorie;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.database_main);
    sqlUpdate = (Button) findViewById(R.id.bSQLUpdate);
    sqlFood = (EditText) findViewById(R.id.etSQLFood);
    sqlCalorie = (EditText) findViewById(R.id.etSQLCalorie);

    sqlView = (Button) findViewById (R.id.bSQLopenView);
    sqlView.setOnClickListener(this);
    sqlUpdate.setOnClickListener(this);

}

public void onClick(View arg0) 
{
    // TODO Auto-generated method stub
    switch (arg0.getId())
    {
    case R.id.bSQLUpdate:

        boolean didItWork = true;
        try
        {
        String food = sqlFood.getText().toString();
        String calorie = sqlCalorie.getText().toString();

        FormDatabase entry = new FormDatabase(DatabaseMain.this);
        entry.open();
        entry.createEntry(food, calorie);
        entry.close();
        }
        catch (Exception e) 
        {
            didItWork = false;
            String error = e.toString();
            Dialog d = new Dialog(this);
            d.setTitle("This is an error!");
            TextView tv = new TextView(this);
            tv.setText(error);
            d.setContentView(tv);
            d.show();
        }
        finally 
        {
            if (didItWork) 
            {
                Dialog d = new Dialog(this);
                d.setTitle("Notice");
                TextView tv = new TextView(this);
                tv.setText("Update success");
                d.setContentView(tv);
                d.show();
            }
        }
        break;

    case R.id.bSQLopenView:
        Intent i = new Intent("com.example.setupdatabase.SQLVIEW");
        startActivity(i);
        break;
    }   
}
}

SQLiteView:

public class SQLView extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sqlview);
    TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
    FormDatabase info = new FormDatabase(this);
    info.open();
    String data = info.getData();
    info.close();
    tv.setText(data);
}
}

SQLView布局xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<TableRow>
    <TextView
    android:text="Food"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">
    </TextView>

    <TextView
    android:text="Calories"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1">
    </TextView>     
</TableRow>
</TableLayout>

<TextView
    android:text="get info from db" 
    android:id="@+id/tvSQLinfo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></TextView>

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="numberSigned" >

    <requestFocus />
</EditText>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

首先:您应该记录您的异常,以便在logcat中获得有用的错误消息和堆栈跟踪。在您发布的问题中包含堆栈跟踪也很有用。

您发布的代码中存在一些错误,例如:

public static final String KEY_FOOD = "food name";

列名中不能包含空格,这会导致SQL语法错误。

db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);

这也是无效的SQL。应为EXISTS而不是EXIST