0

我正在从 csv 文件中读取值,并且能够准确地做到这一点。但是,当我尝试在系统数据库的表中写入这些值时,我收到一条错误消息,指出表中不存在一列。

日志错误:

E/SQLiteLog(318):      (1) table hospital has no column named zip

E/SQLiteDatabase(318): Error inserting zip=36301 avgCharges=20313 avgPayment=4895 _id=10001 address=1108 ROSS CLARK CIRCLE providerName=SOUTHEAST ALABAMA MEDICAL CENTER state=AL procedure=057 - DEGENERATIVE NERVOUS SYSTEM DISORDERS W/O MCC discharges=38 city=DOTHAN

E/SQLiteDatabase(318): android.database.sqlite.SQLiteException: table hospital has no column named zip (code 1): , while compiling: INSERT INTO hospital(zip,avgCharges,avgPayment,_id,address,providerName,state,procedure,discharges,city) VALUES (?,?,?,?,?,?,?,?,?,?)

E/SQLiteDatabase(318):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

创建表查询:(所有字段都在TEXT中进行测试)

query = "CREATE TABLE hospital(_id TEXT PRIMARY KEY, procedure TEXT, providerName TEXT,     address TEXT, city TEXT, state TEXT, zip TEXT, discharges TEXT, avgCharges TEXT, avgPayment TEXT)";

db.execSQL(query);

我确信没有列类型不匹配。它说它找不到名为 ZIP 的列。我不明白这里发生了什么。

查询插入值:values.put("_id", hospital.get_id());

    values.put("procedure", hospital.get_procedure());
    values.put("providerName", hospital.get_providerName());
    values.put("address", hospital.get_address());
    values.put("city", hospital.get_city());
    values.put("state", hospital.get_state());
    values.put("zip", hospital.get_zip());
    values.put("discharges", hospital.get_discharges());
    values.put("avgCharges", hospital.get_avgCharges());
    values.put("avgPayment", hospital.get_avgPayment());

    db.insert(TABLE_NAME, null, values);

关于可以在这里做什么的任何想法?提前致谢!

4

2 回答 2

0

您需要实现 onUpgrade 方法以在架构更改时删除和添加表。您可以查看https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#onUpgrade(android.database.sqlite.SQLiteDatabase,%20int,%20int)

于 2013-08-12T21:45:19.380 回答
0

在代码中添加新列之前,需要删除该表。您应该清除应用程序数据。否则卸载该应用程序并从 Studio 运行它。

于 2017-05-14T14:38:06.647 回答