我正在尝试使用sqlite作为数据库更新Android应用程序上的元素。但我收到错误SQLite异常:没有这样的列。
我的logcat是:
04-15 18:46:19.277 5290-5290/com.example.testlayout.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.testlayout.app, PID: 5290
android.database.sqlite.SQLiteException: no such column: id (code 1): , while compiling: UPDATE pontos SET Estado=?,Armadura=?,Observações=?,Serial=?,NrColuna=?,Rede=?,Lampada=?,tir=?,Y=?,Potencia=?,X=?,Coluna=? WHERE id=?
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1572)
at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1520)
at com.example.testlayout.app.PointsDBAdapter.updatePoint(PointsDBAdapter.java:85)
at com.example.testlayout.app.CreatePoint$1.onClick(CreatePoint.java:75)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
还将与我分享我的publicAdapter,其中包含更新类:
public class PointsDBAdapter {
private SQLiteDatabase database;
private PointsDB points;
private String[] allColumns = {PointsDB.ID, PointsDB.SERIAL, PointsDB.OBSERVATIONS, PointsDB.TIR, PointsDB.X, PointsDB.Y,
PointsDB.NETWORKNUMBER, PointsDB.POTENCIA, PointsDB.COLUMNNR, PointsDB.COLUNA, PointsDB.LAMP, PointsDB.ARMOR, PointsDB.LAMPSTATE};
public PointsDBAdapter(Context context) {
points = new PointsDB(context);
open();
}
public void open() throws SQLException {
database = points.getWritableDatabase();
}
public Point createPoint(String Serial, String Observation, boolean tir, double x, double y, int networkNumber, int intensity,
int ColumnNr, int Column, int lamp, int armor, int lampState) {
ContentValues values = new ContentValues();
values.put(PointsDB.SERIAL, Serial);
values.put(PointsDB.OBSERVATIONS, Observation);
values.put(PointsDB.TIR, tir);
values.put(PointsDB.X, x);
values.put(PointsDB.Y, y);
values.put(PointsDB.NETWORKNUMBER, networkNumber);
values.put(PointsDB.POTENCIA, intensity);
values.put(PointsDB.COLUMNNR, ColumnNr);
values.put(PointsDB.COLUNA, Column);
values.put(PointsDB.LAMP, lamp);
values.put(PointsDB.ARMOR, armor);
values.put(PointsDB.LAMPSTATE, lampState);
long insertId = database.insert(PointsDB.TABLE_NAME, null, values);
// To show how to query
Cursor cursor = database.query(PointsDB.TABLE_NAME, allColumns, PointsDB.ID + " = " +
insertId, null,null, null, null);
cursor.moveToFirst();
return cursorToPoint(cursor);
}
private Point cursorToPoint(Cursor cursor) {
Point point = new
Point(cursor.getString(1), cursor.getString(2), Boolean.parseBoolean(cursor.getString(3)), Double.parseDouble(cursor.getString(4)),
Double.parseDouble(cursor.getString(5)), Integer.parseInt(cursor.getString(6)), Integer.parseInt(cursor.getString(7)),
Integer.parseInt(cursor.getString(8)), Integer.parseInt(cursor.getString(9)), Integer.parseInt(cursor.getString(10)),
Integer.parseInt(cursor.getString(11)), Integer.parseInt(cursor.getString(12)));
return point;
}
public void updatePoint(int idPoint, String Serial, String Observation, boolean tir, double x, double y, int networkNumber, int intensity,
int ColumnNr, int Column, int lamp, int armor, int lampState){
String where = "id=?";
String[] whereArgs = new String[] {String.valueOf(idPoint)};
ContentValues values = new ContentValues();
values.put(PointsDB.SERIAL, Serial);
values.put(PointsDB.OBSERVATIONS, Observation);
values.put(PointsDB.TIR, tir);
values.put(PointsDB.X, x);
values.put(PointsDB.Y, y);
values.put(PointsDB.NETWORKNUMBER, networkNumber);
values.put(PointsDB.POTENCIA, intensity);
values.put(PointsDB.COLUMNNR, ColumnNr);
values.put(PointsDB.COLUNA, Column);
values.put(PointsDB.LAMP, lamp);
values.put(PointsDB.ARMOR, armor);
values.put(PointsDB.LAMPSTATE, lampState);
database.update(PointsDB.TABLE_NAME, values, where, whereArgs);
}
public Point getPoint(int idPoint){
Log.d("tag", "estou a entrar com um valor de idPoint = " + idPoint);
Cursor cursor = database.query(PointsDB.TABLE_NAME, allColumns, PointsDB.ID + " = " +
idPoint, null,null, null, null);
if(cursor.moveToFirst()){
return cursorToPoint(cursor);
} else
return null;
}
public long count() {
return DatabaseUtils.queryNumEntries(database,PointsDB.TABLE_NAME);
}
}
这是我的DBhelper类:
public class PointsDB extends SQLiteOpenHelper {
public static final String ID = "_id";
public static final String SERIAL = "Serial";
public static final String OBSERVATIONS = "Observações";
public static final String TIR = "tir";
public static final String X = "X";
public static final String Y = "Y";
public static final String NETWORKNUMBER = "Rede";
public static final String POTENCIA = "Potencia";
public static final String COLUMNNR = "NrColuna";
public static final String COLUNA = "Coluna";
public static final String LAMP = "Lampada";
public static final String ARMOR = "Armadura";
public static final String LAMPSTATE = "Estado";
private static final int DATABASE_VERSION = 3;
private static final String DATABASE_NAME = "pontos.db";
public static final String TABLE_NAME = "pontos";
private static final String DATABASE_CREATE = "create table "
+ TABLE_NAME + "( " + ID
+ " integer primary key autoincrement, " + SERIAL
+ " text not null, " + OBSERVATIONS + " text not null, "
+ TIR + " text not null" +
", " + X + " text not null, " + Y + " text not null, " + NETWORKNUMBER + " text not null, " + POTENCIA + " text not null, "
+ COLUMNNR + " text not null, " + COLUNA + " text not null, " + LAMP + " text not null, "
+ ARMOR + " text not null, " + LAMPSTATE + " text not null);";
public PointsDB(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(PointsDB.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data"
);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
最后,我在我的主类中调用该方法:
public void onClick(View view) {
String Serial = SerialField.getText().toString();
String Observation = ObservationField.getText().toString();
double X = Double.parseDouble(XField.getText().toString());
double Y = Double.parseDouble(YField.getText().toString());
int columnNumber = Integer.parseInt(columnNrField.getText().toString());
int intensity = Integer.parseInt(intensityField.getText().toString());
int networkToSet = networkSpinner.getSelectedItemPosition();
boolean checkBoxStatus = status.isChecked();
int valToSet = spinner.getSelectedItemPosition();
int setColumn = columnSpinner.getSelectedItemPosition();
int setArmor = armor.getSelectedItemPosition();
int setLamp = lamp.getSelectedItemPosition();
pointsList.updatePoint(pos, Serial, Observation, checkBoxStatus, X, Y, networkToSet, columnNumber, intensity, setColumn, setArmor, setLamp, valToSet);
答案 0 :(得分:2)
我看到你的列名是_id而不是id。]
你可以在这里查看。
public static final String ID = "_id";
在这里
private static final String DATABASE_CREATE = "create table "
+ TABLE_NAME + "( " + ID
+ " integer primary key autoincrement, " + SERIAL
+ " text not null, " + OBSERVATIONS + " text not null, "
+ TIR + " text not null" +
", " + X + " text not null, " + Y + " text not null, " + NETWORKNUMBER + " text not null, " + POTENCIA + " text not null, "
+ COLUMNNR + " text not null, " + COLUNA + " text not null, " + LAMP + " text not null, "
+ ARMOR + " text not null, " + LAMPSTATE + " text not null);";
但是,您正在搜索id列而不是_id列
public void updatePoint(int idPoint, String Serial, String Observation, boolean tir, double x, double y, int networkNumber, int intensity,
int ColumnNr, int Column, int lamp, int armor, int lampState){
String where = "id=?";
只需改变其中的位置,来自&#34; id =?&#34;到&#34; _id =?&#34;