gpt4 book ai didi

android 和 sqlite-有 5 列,但提供了 4 个值

转载 作者:IT王子 更新时间:2023-10-29 06:28:06 26 4
gpt4 key购买 nike

我从某处复制了一个使用sqlite的例子。他们定义了 5 列,我将其更改为 4 列。相关部分在这里:

    private final String DB_NAME = "mcustomerDbName";
private final String TABLE_NAME = "mcustomeTableName";
SQLiteDatabase customersDB = null;
/** Called when the activity is first created. */


..........................................

这是表 4 列的上下文

    private void mFillDbsTable(){
try {

customersDB.execSQL("INSERT INTO " +
TABLE_NAME +
" Values ('ABARTH','500','Abarth','15.3846153846154' );");
customersDB.execSQL("INSERT INTO " + TABLE_NAME + "Values ( 'ABARTH ', 'Grande Punto ', '1.4 T-Jet 155 ', '14.9253731343284 ' );");
customersDB.execSQL("INSERT INTO " + TABLE_NAME + "Values ( 'ALFA ROMEO ', 'MiTo ', '1.4 16v TB 120 ', '16.3934426229508 ');");
customersDB.execSQL("INSERT INTO " + TABLE_NAME + "Values ( 'ALFA ROMEO ', 'MiTo ', '1.3 JTDm 90 ', '22.2222222222222 ');");
customersDB.execSQL("INSERT INTO " + TABLE_NAME + "Values ( 'ALFA ROMEO ', '147 ', '1.6 (120 bhp) ', '12.1951219512195 ');");
customersDB.execSQL("INSERT INTO " + TABLE_NAME + "Values ( 'ALFA ROMEO ', '147 ', '2.0 ', '11.2359550561798 ');");

..........................................

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*setContentView(R.layout.activity_delek);**/
ArrayList<String> results = new ArrayList<String>();
try {
customersDB = this.openOrCreateDatabase( DB_NAME, MODE_PRIVATE, null);
customersDB.execSQL("CREATE TABLE IF NOT EXISTS " +
TABLE_NAME +
" (Name VARCHAR," +
" Street VARCHAR, Block VARCHAR, City VARCHAR);");
mFillDbsTable();
Cursor c = customersDB.rawQuery("SELECT Name, Street, Block, City FROM " +
TABLE_NAME +
" where Name == 'TOYOTA' or Name == 'Yosi' ", null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String name = c.getString(c.getColumnIndex("Name"));
String street = c.getString(c.getColumnIndex("Street"));

String city = c.getString(c.getColumnIndex("City"));

results.add(name + ", " + street + " "+ city );
}while (c.moveToNext());
}
}
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "create/Open the database problem");
} finally {
if (customersDB != null)
customersDB.execSQL("DELETE FROM " + TABLE_NAME);
customersDB.close();
}

我在日志中收到此消息:

 10-01 21:59:59.434: I/Database(1067): sqlite returned: error code = 1, msg = table            mcustomeTableName has 5 columns but 4 values were supplied.

为什么??我有 4 列

最佳答案

该表可能在之前的运行中已经存在,没有您的更改,并且在数据库中仍然有 5 列(如果不存在则创建表)。您可以通过从 ADP 登录到 sqllite 来验证这一点。只需在运行您的应用程序之前放下表格即可。

此外,为您的插入语句提供一个列列表。无论如何,这都是一种很好的做法,因为您的应用程序对于模式更改将更加健壮:

"INSERT INTO " + TABLE_NAME + " (Name, Street, Block, City) Values (" ...etc

关于android 和 sqlite-有 5 列,但提供了 4 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12681998/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com