Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android SQLite数据库表中的数据项未显示_Android_Sqlite_Android Tablelayout - Fatal编程技术网

Android SQLite数据库表中的数据项未显示

Android SQLite数据库表中的数据项未显示,android,sqlite,android-tablelayout,Android,Sqlite,Android Tablelayout,我正试图建立一个SQLite数据库,其中包含一些关于食物及其热值的信息。然而,当我输入信息,然后点击按钮查看时,表格什么也没有显示。我刚才输入的信息没有显示。 我看不出问题出在哪里。需要帮助查看代码并让我知道那里出了什么问题 创建数据库: public class FormDatabase { public static final String KEY_ROWID = "_id"; public static final String KEY_FOOD = "food name"; publ

我正试图建立一个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;
    }   
}
}
更新并查看数据库:

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>

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

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

public static final String KEY_FOOD = "food name";
列名中不能有空格,这将导致SQL语法错误

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

这也是无效的SQL。应该是
存在
不是
存在

谢谢你的建议。我刚刚编辑过。现在它给了我一个错误,当我试图查看数据时。在LogCat检查时说,这是因为“食品名称”不存在。我应该如何纠正这个问题?