我的数据库包含音乐会列表,我根据名称列更新它们。单击更新按钮时,将传入给定Concert的列值。但是,仅当name列为单个数字时,更新才有效。如果name列是一个字符串,或2位数字,它会立即崩溃并给我以下错误。
11-17 13:14:51.325 12131-12131/com.example.msdproject I/test﹕ Update artist method: Value of name = test
11-17 13:14:51.325 12131-12131/com.example.msdproject I/test﹕ Update artist method: Value of venue = test venue
11-17 13:14:51.325 12131-12131/com.example.msdproject I/test﹕ Update artist method: Value of date = dd
11-17 13:14:51.325 12131-12131/com.example.msdproject I/test﹕ Update artist method: Value of comments = d
11-17 13:14:51.325 12131-12131/com.example.msdproject I/test﹕ DB Manager method: Value of name = test
11-17 13:14:51.325 12131-12131/com.example.msdproject I/test﹕ DB Manager method: Value of venue = test venue
11-17 13:14:51.325 12131-12131/com.example.msdproject I/test﹕ DB Manager method: Value of date = dd
11-17 13:14:51.325 12131-12131/com.example.msdproject I/test﹕ DB Manager method: Value of comments = d
11-17 13:14:51.325 12131-12131/com.example.msdproject E/SQLiteLog﹕ (1) no such column: test
11-17 13:14:51.335 12131-12131/com.example.msdproject D/AndroidRuntime﹕ Shutting down VM
11-17 13:14:51.335 12131-12131/com.example.msdproject W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41614ba8)
11-17 13:14:51.355 12131-12131/com.example.msdproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.msdproject, PID: 12131
android.database.sqlite.SQLiteException: no such column: test (code 1): , while compiling: UPDATE Concert_Info SET date=?,venue=?,comments=?,name=? WHERE name=test
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.msdproject.DBManager.updateConcert(DBManager.java:150)
at com.example.msdproject.UpdateArtist.UpdateArtistButton(UpdateArtist.java:103)
at com.example.msdproject.UpdateArtist$1.onClick(UpdateArtist.java:79)
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:5001)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
11-17 13:14:51.365 12131-12131/com.example.msdproject D/dalvikvm﹕ GC_FOR_ALLOC freed 591K, 4% free 17167K/17788K, paused 9ms, total 9ms
11-17 13:14:53.245 12903-12903/com.example.msdproject I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
11-17 13:14:53.275 12903-12903/com.example.msdproject D/OpenGLRenderer﹕ Enabling debug mode 0
基本上说的是该列不存在,但我不明白为什么它会给我这个错误,因为当我使用单个数字时它识别出列存在。我尝试通过_id删除,但这给了我相同的结果,所以我恢复到名称列删除。是什么可以传递到数据库更新功能?
这是updateArtist类:
package com.example.msdproject;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.sql.SQLException;
public class UpdateArtist extends Activity {
DBManager db = new DBManager(this);
public EditText nameTxt;
public EditText venueTxt;
public EditText dateTxt;
public EditText commentsTxt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update_artist);
Intent intent = getIntent();
final long num = intent.getLongExtra("id", 1);
try {
db.open();
Cursor c = db.getConcert(num);
if (c.moveToFirst())
{
do
{
String name = (c.getString(1));
String venue = (c.getString(2));
String date = (c.getString(3));
String comments = (c.getString(4));
TextView titleTxt = (TextView)findViewById(R.id.updateTitle);
nameTxt = (EditText)findViewById(R.id.updateName);
venueTxt = (EditText)findViewById(R.id.updateVenue);
dateTxt = (EditText)findViewById(R.id.updateDate);
commentsTxt = (EditText)findViewById(R.id.updateComments);
titleTxt.setText(name);
nameTxt.setText(name);
venueTxt.setText(venue);
dateTxt.setText(date);
commentsTxt.setText(comments);
Button update = (Button)findViewById(R.id.updateConcertButton);
update.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (nameTxt.getText().toString().isEmpty() || venueTxt.getText().toString().isEmpty() ||
dateTxt.getText().toString().isEmpty() || commentsTxt.getText().toString().isEmpty() )
{
Toast.makeText(UpdateArtist.this, "Error! Please fill in all fields", Toast.LENGTH_LONG).show();
}
else
{
String updatedName = nameTxt.getText().toString();
String updatedVenue = venueTxt.getText().toString();
String updatedDate = dateTxt.getText().toString();
String updatedComments = commentsTxt.getText().toString();
UpdateArtistButton(updatedName,updatedVenue,updatedDate,updatedComments);
}
}
});
} while (c.moveToNext());
}
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public void UpdateArtistButton(String passedName, String passedVenue, String passedDate, String passedComments)
{
try
{
db.open();
Log.i("test", "Update artist method: Value of name = " + passedName);
Log.i("test", "Update artist method: Value of venue = " + passedVenue);
Log.i("test", "Update artist method: Value of date = " + passedDate);
Log.i("test", "Update artist method: Value of comments = " + passedComments);
db.updateConcert(passedName,passedVenue, passedDate, passedComments);
Toast.makeText(UpdateArtist.this, "Concert Updated!", Toast.LENGTH_LONG).show();
super.finish();
}
catch (SQLException e)
{
e.printStackTrace();
}
db.close();
}
}
这是DBManager类:
package com.example.msdproject;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import android.util.Log;
import java.sql.SQLException;
public class DBManager {
public static final String COL_ROWID = "_id";
public static final String COL_NAME = "name";
public static final String COL_VENUE = "venue";
public static final String COL_DATE = "date";
public static final String COL_COMMENTS = "comments";
private static final String DB_NAME = "Concerts";
private static final String DB_TABLE = "Concert_Info";
private static final int DB_VERSION = 1;
private static final String DB_CREATE =
"create table " + DB_TABLE +
" (_id integer primary key autoincrement, " +
"name text not null, " +
"venue text not null, " +
"comments text not null, " +
"date text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBManager(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
public static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DB_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
/*
//taken from YouTube Tutorial video
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("drop table if exists Concert_Info");
onCreate(db);*/
}
}
public DBManager open() throws SQLException
{
db = DBHelper.getWritableDatabase();
//db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE + ";");
return this;
}
public void close()
{
DBHelper.close();
}
public long insertConcert(String name, String venue, String date, String comments)
{
ContentValues initialValues = new ContentValues();
initialValues.put(COL_NAME, name);
initialValues.put(COL_VENUE, venue);
initialValues.put(COL_DATE, date);
initialValues.put(COL_COMMENTS, comments);
return db.insert(DB_TABLE, null, initialValues);
}
public boolean deleteConcert(String name)
{
return db.delete(DB_TABLE, COL_NAME + "=" + name, null) > 0;
}
public Cursor getAllConcerts() {
return db.query(DB_TABLE, new String[]
{
COL_ROWID,
COL_NAME,
COL_VENUE,
COL_DATE,
COL_COMMENTS
},
null,
null,
null,
null,
null,
null
);
}
public Cursor getConcert(long ROW_ID) throws SQLException
{
Cursor mCursor =
db.query(DB_TABLE, new String[]
{
COL_ROWID,
COL_NAME,
COL_VENUE,
COL_DATE,
COL_COMMENTS
},
COL_ROWID + "=" + ROW_ID,
null,
null,
null,
null
);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateConcert(String name, String venue, String date, String comments)
{
ContentValues updateValues = new ContentValues();
updateValues.put(COL_NAME, name);
updateValues.put(COL_VENUE, venue);
updateValues.put(COL_DATE, date);
updateValues.put(COL_COMMENTS, comments);
Log.i("test", "DB Manager method: Value of name = " + name);
Log.i("test", "DB Manager method: Value of venue = " + venue);
Log.i("test", "DB Manager method: Value of date = " + date);
Log.i("test", "DB Manager method: Value of comments = " + comments);
return db.update(DB_TABLE, updateValues, "name" + "=" + name, null) > 0;
}
public void reset () throws SQLException {
db.delete(DB_TABLE, null, null);
db.close();
this.DBHelper.onCreate(this.db);
}
}
答案 0 :(得分:0)
值:
return db.update(DB_TABLE, updateValues, "name" + "='" + name +"'", null) > 0
答案 1 :(得分:0)
代码中的问题是: WHERE name = test
您的查询已损坏。
测试字段的值或名称吗?取决于测试的语义,你有3种方法来解决这个问题:
如果它是sqlite-field的名称,那么你的表缺少这个字段,你应该把它添加到你的sqlite表中。
应该测试一个值,然后你必须使用&#39;围绕着这个。
如果test是java代码中的变量,则必须将此变量的值插入到具有串联f.e。