无法在 SQLite Android Eclipse 中创建超过 2 列?
Posted
技术标签:
【中文标题】无法在 SQLite Android Eclipse 中创建超过 2 列?【英文标题】:Unable to make more than 2 columns in SQLite Android Eclipse? 【发布时间】:2015-03-24 00:45:26 【问题描述】:为什么当我的列超过 3 列时,应用程序会崩溃?我尝试通过注释掉一列来调试它,它运行得很好。在对其进行了更多调试后,我发现问题可能出在“populateListview”上,因为我能够运行该应用程序,但现在它不会显示任何内容。为什么你认为我不能用超过 3 列运行它?
这是我的 dbAdapter:
// Field Names:
public static final String KEY_ROWID = "_id";
public static final String KEY_INGREDIENTNAME = "ingredientname";
public static final String KEY_IMAGE = "image";
public static final String KEY_DETAILS = "details";
public static final String[] ALL_KEYS = new String[] KEY_ROWID, KEY_INGREDIENTNAME, KEY_IMAGE, KEY_DETAILS;
// Column Numbers for each Field Name:
public static final int COL_ROWID = 0;
public static final int COL_INGREDIENTNAME = 1;
public static final int COL_IMAGE = 2;
public static final int COL_DETAILS = 3;
//SQL statement to create database
private static final String DATABASE_CREATE_SQL =
"CREATE TABLE " + DATABASE_TABLE
+ " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_INGREDIENTNAME + " TEXT NOT NULL, "
+ KEY_IMAGE + " TEXT"
+ KEY_DETAILS + " TEXT"
+ ");";
// Add a new set of values to be inserted into the database.
public long insertRow(String ingredientname, String image, String detailsValue)
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_INGREDIENTNAME, ingredientname);
initialValues.put(KEY_IMAGE, image);
initialValues.put(KEY_DETAILS, detailsValue);
// Insert the data into the database.
return db.insert(DATABASE_TABLE, null, initialValues);
// Return all data in the database.
public Cursor getAllRows()
String where = null;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS, where, null, null, null, null, null);
if (c != null)
c.moveToFirst();
return c;
这是我的 populateListView 代码:
private void populateListView()
Cursor cursor = myDb.getAllRows();
String[] fromFieldNames = new String[] //DBAdapter.KEY_ROWID,
DBAdapter.KEY_INGREDIENTNAME ;
int[] toViewIDs = new int[] //R.id.textViewItemNumber,
R.id.textViewItemTask ;
SimpleCursorAdapter myCursorAdapter;
myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),
R.layout.item_layout, cursor, fromFieldNames, toViewIDs, 0);
ListView myList = (ListView) findViewById(R.id.listViewTask);
myList.setAdapter(myCursorAdapter);
【问题讨论】:
您在日志中遇到的错误是什么。发布您的日志和导致错误的行 【参考方案1】:你错过了一个逗号,这里:
private static final String DATABASE_CREATE_SQL =
"CREATE TABLE " + DATABASE_TABLE
+ " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_INGREDIENTNAME + " TEXT NOT NULL, "
+ KEY_IMAGE + " TEXT"
+ KEY_DETAILS + " TEXT"
+ ");";
应该是
private static final String DATABASE_CREATE_SQL =
"CREATE TABLE " + DATABASE_TABLE
+ " (" + KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_INGREDIENTNAME + " TEXT NOT NULL, "
+ KEY_IMAGE + " TEXT,"
+ KEY_DETAILS + " TEXT"
+ ");";
【讨论】:
为什么?这只是分心。以上是关于无法在 SQLite Android Eclipse 中创建超过 2 列?的主要内容,如果未能解决你的问题,请参考以下文章
Android SQLite 更新无法在 AsyncTask 的后台运行