我正在创建一个我正在创建的应用程序的问题,当我更新记录时单击保存按钮时出现问题。
这是我的Java文件。
import com.androidadvance.db.DatabaseHelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AddUpdateValues extends Activity implements OnClickListener {
private Button btn_updaterecord;
private EditText txtpname, txtpprice;
DatabaseHelper db;
ProductModel pm;
Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addupdatevalues);
i = getIntent();
txtpname = (EditText) findViewById(R.id.txt_udatepname);
txtpprice = (EditText) findViewById(R.id.txt_udatepprice);
txtpname.setText(i.getExtras().getString("name"));
txtpprice.setText(i.getExtras().getString("price"));
btn_updaterecord = (Button) findViewById(R.id.btn_updaterecord);
btn_updaterecord.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_updaterecord:
if (txtpname.getText().toString().equals("")
|| txtpprice.getText().toString().equals("")) {
Toast.makeText(AddUpdateValues.this, "Please add values..",
Toast.LENGTH_LONG).show();
} else {
db = new DatabaseHelper(getApplicationContext());
db.getWritableDatabase();
pm = new ProductModel();
pm.productname = txtpname.getText().toString();
pm.productprice = txtpprice.getText().toString();
pm.idno = i.getExtras().getString("id");
Log.i(">>>>>productid<<<<<", "" + i.getExtras().getString("id"));
db.updateProduct(pm);
Toast.makeText(AddUpdateValues.this,
"Room Update successfully.", Toast.LENGTH_LONG).show();
db.close();
super.onResume();
}
break;
}
}
}
这是xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/background" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Update Room Length:" />
<EditText
android:id="@+id/txt_udatepname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:hint="Room Length" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Update Room Width:" />
<EditText
android:id="@+id/txt_udatepprice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:hint="Room Width" />
<Button
android:id="@+id/btn_updaterecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Save Changes" />
</LinearLayout>
Databasehelper
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.homediyassistant.screen.ProductModel;
public class DatabaseHelper extends SQLiteOpenHelper {
public static String DATABASENAME = "androidadvancesqlite";
public static String PRODUCTTABLE = "products";
public static String colProductId = "id";
public static String _colProductid = "productidno";
public static String colProductName = "productname";
public static String colProductPrice = "productprice";
private ArrayList<ProductModel> cartList = new ArrayList<ProductModel>();
Context c;
public DatabaseHelper(Context context) {
super(context, DATABASENAME, null, 33);
c = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
// db.execSQL("CREATE TABLE if not exists " + PRODUCTTABLE + "("
// + colProductId + " INTEGER PRIMARY KEY AUTOINCREMENT , "
// + "productidno" + "TEXT," + colProductName + " TEXT ,"
// + colProductPrice + " TEXT)");
db.execSQL("CREATE TABLE if not exists producttable(id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "productidno"
+ " TEXT ,"
+ "productname"
+ " TEXT,"
+ "productprice" + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + PRODUCTTABLE);
onCreate(db);
}
public void addProduct(ProductModel productitem) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("productidno", productitem.idno);
contentValues.put("productname", productitem.productname);
contentValues.put("productprice", productitem.productprice);
db.insert("producttable", null, contentValues);
db.close();
}
// update
public void updateProduct(ProductModel productList) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("productname", productList.productname);
contentValues.put("productprice", productList.productprice);
db.update("producttable", contentValues, "productidno="
+ productList.idno, null);
db.close();
}
public void emptyProduct() {
try {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("delete from producttable");
db.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void removeProduct(String productid, String pname, String pprice) {
try {
// SQLiteDatabase db = this.getWritableDatabase();
// db.execSQL("delete from producttable where productidno="
// + productid);
// db.close();
String[] args = { productid };
getWritableDatabase().delete("producttable", "productidno=?", args);
} catch (Exception e) {
e.printStackTrace();
}
}
public ArrayList<ProductModel> getProudcts() {
cartList.clear();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from producttable", null);
if (cursor.getCount() != 0) {
if (cursor.moveToFirst()) {
do {
ProductModel item = new ProductModel();
item.idno = cursor.getString(cursor
.getColumnIndex("productidno"));
item.productname = cursor.getString(cursor
.getColumnIndex("productname"));
item.productprice = cursor.getString(cursor
.getColumnIndex("productprice"));
cartList.add(item);
} while (cursor.moveToNext());
}
}
cursor.close();
db.close();
return cartList;
}
}
这是我的logcat
05-03 12:10:09.323: E/SensorManager(3963): thread start
05-03 12:10:09.898: E/SpannableStringBuilder(3963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-03 12:10:09.898: E/SpannableStringBuilder(3963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-03 12:10:17.498: E/SpannableStringBuilder(3963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-03 12:10:17.498: E/SpannableStringBuilder(3963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-03 12:10:20.868: E/SpannableStringBuilder(3963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-03 12:10:20.868: E/SpannableStringBuilder(3963): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-03 12:10:38.173: E/SQLiteLog(3963): (1) no such column: fff
05-03 12:10:38.193: E/AndroidRuntime(3963): FATAL EXCEPTION: main
05-03 12:10:38.193: E/AndroidRuntime(3963): android.database.sqlite.SQLiteException: no such column: fff (code 1): , while compiling: UPDATE producttable SET productprice=?,productname=? WHERE productidno=fff
05-03 12:10:38.193: E/AndroidRuntime(3963): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
05-03 12:10:38.193: E/AndroidRuntime(3963): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1038)
05-03 12:10:38.193: E/AndroidRuntime(3963): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:649)
05-03 12:10:38.193: E/AndroidRuntime(3963): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
05-03 12:10:38.193: E/AndroidRuntime(3963): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
05-03 12:10:38.193: E/AndroidRuntime(3963): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
05-03 12:10:38.193: E/AndroidRuntime(3963): at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1563)
05-03 12:10:38.193: E/AndroidRuntime(3963): at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1514)
05-03 12:10:38.193: E/AndroidRuntime(3963): at com.homediyassistant.db.DatabaseHelper.updateProduct(DatabaseHelper.java:68)
05-03 12:10:38.193: E/AndroidRuntime(3963): at com.homediyassistant.screen.AddUpdateValues.onClick(AddUpdateValues.java:59)
05-03 12:10:38.193: E/AndroidRuntime(3963): at android.view.View.performClick(View.java:4261)
05-03 12:10:38.193: E/AndroidRuntime(3963): at android.view.View$PerformClick.run(View.java:17356)
05-03 12:10:38.193: E/AndroidRuntime(3963): at android.os.Handler.handleCallback(Handler.java:615)
05-03 12:10:38.193: E/AndroidRuntime(3963): at android.os.Handler.dispatchMessage(Handler.java:92)
05-03 12:10:38.193: E/AndroidRuntime(3963): at android.os.Looper.loop(Looper.java:137)
05-03 12:10:38.193: E/AndroidRuntime(3963): at android.app.ActivityThread.main(ActivityThread.java:4921)
05-03 12:10:38.193: E/AndroidRuntime(3963): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 12:10:38.193: E/AndroidRuntime(3963): at java.lang.reflect.Method.invoke(Method.java:511)
05-03 12:10:38.193: E/AndroidRuntime(3963): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
05-03 12:10:38.193: E/AndroidRuntime(3963): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
05-03 12:10:38.193: E/AndroidRuntime(3963): at dalvik.system.NativeStart.main(Native Method)
我无法将手指放在掉落的地方。
非常感谢您的帮助。
由于
答案 0 :(得分:0)
查看您的logcat,您的应用程序似乎正在崩溃,因为您输入的是表中不存在的“id”(productidno)。输入有效的“id”或检查正在输入的“id”。
我希望这能解决你的问题。