gpt4 book ai didi

java - 数据 未存储数据

转载 作者:行者123 更新时间:2023-12-01 23:15:56 25 4
gpt4 key购买 nike

我输入了数据,但为什么我无法存储在我的数据库中,是代码有问题吗?帮助。多谢。

 01-19 17:55:40.953: E/Database(332): Error inserting itemdesc=wew itemquantity=1 itemname=wew
01-19 17:55:40.953: E/Database(332): android.database.sqlite.SQLiteException: table inventory_db has no column named itemquantity: , while compiling: INSERT INTO inventory_db(itemdesc, itemquantity, itemname) VALUES(?, ?, ?);
01-19 17:55:40.953: E/Database(332): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
01-19 17:55:40.953: E/Database(332): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
01-19 17:55:40.953: E/Database(332): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
01-19 17:55:40.953: E/Database(332): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
01-19 17:55:40.953: E/Database(332): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
01-19 17:55:40.953: E/Database(332): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149)
01-19 17:55:40.953: E/Database(332): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1569)
01-19 17:55:40.953: E/Database(332): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
01-19 17:55:40.953: E/Database(332): at com.example.aoutsu.AddNewItem.saveData(AddNewItem.java:96)
01-19 17:55:40.953: E/Database(332): at com.example.aoutsu.AddNewItem.onClick(AddNewItem.java:57)
01-19 17:55:40.953: E/Database(332): at android.view.View.performClick(View.java:2485)
01-19 17:55:40.953: E/Database(332): at android.view.View$PerformClick.run(View.java:9080)
01-19 17:55:40.953: E/Database(332): at android.os.Handler.handleCallback(Handler.java:587)
01-19 17:55:40.953: E/Database(332): at android.os.Handler.dispatchMessage(Handler.java:92)
01-19 17:55:40.953: E/Database(332): at android.os.Looper.loop(Looper.java:123)
01-19 17:55:40.953: E/Database(332): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-19 17:55:40.953: E/Database(332): at java.lang.reflect.Method.invokeNative(Native Method)
01-19 17:55:40.953: E/Database(332): at java.lang.reflect.Method.invoke(Method.java:507)
01-19 17:55:40.953: E/Database(332): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-19 17:55:40.953: E/Database(332): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-19 17:55:40.953: E/Database(332): at dalvik.system.NativeStart.main(Native Method)

这是我的 AddNewItem.java

package com.example.aoutsu;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AddNewItem extends Activity implements OnClickListener{

private Button btn_save;
private EditText edit_name,edit_desc,edit_quantity;
private DbHelper mHelper;
private SQLiteDatabase dataBase;
private String id,name,desc,quantity;
private boolean isUpdate;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_item);

btn_save=(Button)findViewById(R.id.save_btn);
edit_name=(EditText)findViewById(R.id.item_name);
edit_desc=(EditText)findViewById(R.id.item_desc);
edit_quantity=(EditText)findViewById(R.id.Quantity);
isUpdate=getIntent().getExtras().getBoolean("update");
if(isUpdate)
{
id=getIntent().getExtras().getString("ID");
name=getIntent().getExtras().getString("ItemName");
desc=getIntent().getExtras().getString("ItemDesc");
quantity=getIntent().getExtras().getString("Quantity");
edit_name.setText(name);
edit_desc.setText(desc);
edit_quantity.setText(quantity);

}

btn_save.setOnClickListener(this);

mHelper=new DbHelper(this);

}

// saveButton click event
public void onClick(View v) {
name=edit_name.getText().toString().trim();
desc=edit_desc.getText().toString().trim();
quantity=edit_quantity.getText().toString().trim();
if(name.length()>0 && desc.length()>0 && quantity.length()>0 )
{
saveData();
}
else
{
AlertDialog.Builder alertBuilder=new AlertDialog.Builder(AddNewItem.this);
alertBuilder.setTitle("Invalid Data");
alertBuilder.setMessage("Please, Enter valid data");
alertBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
dialog.cancel();

}
});
alertBuilder.create().show();
}

}

/**
* save data into SQLite
*/
private void saveData(){
dataBase=mHelper.getWritableDatabase();
ContentValues values=new ContentValues();

values.put(DbHelper.KEY_ITEMNAME,name);
values.put(DbHelper.KEY_ITEMDESC,desc );
values.put(DbHelper.KEY_ITEMQUANTITY,quantity );

System.out.println("");
if(isUpdate)
{
//update database with new data
dataBase.update(DbHelper.TABLE_NAME, values, DbHelper.KEY_ID+"="+id, null);
}
else
{
//insert data into database
dataBase.insert(DbHelper.TABLE_NAME, null, values);
}
//close database
dataBase.close();
finish();


}

}

我的DbHelper.java

package com.example.aoutsu;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* sqlite database helper to create table into SQLite database
* @author ketan(Visit my <a
* href="http://androidsolution4u.blogspot.in/">blog</a>)
*/
public class DbHelper extends SQLiteOpenHelper {
static String DATABASE_NAME="example_db";
public static final String TABLE_NAME="inventory_db";
public static final String KEY_ITEMNAME="itemname";
public static final String KEY_ITEMDESC="itemdesc";
public static final String KEY_ITEMQUANTITY="itemquantity";
public static final String KEY_ID="id";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, 1);

}

@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("+KEY_ID+" INTEGER PRIMARY KEY, "+KEY_ITEMNAME+" TEXT, "+KEY_ITEMDESC+" TEXT, "+KEY_ITEMQUANTITY+ "TEXT)";
db.execSQL(CREATE_TABLE);

}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);

}

}

DisplayAdapter.java

package com.example.aoutsu;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


public class DisplayAdapter extends BaseAdapter{
private Context mContext;
private ArrayList<String> id;
private ArrayList<String> item;
private ArrayList<String> description;
private ArrayList<String> quantity;


public DisplayAdapter(Context c, ArrayList<String> id,ArrayList<String> itemname, ArrayList<String> itemdesc, ArrayList<String> itemquantity ) {
this.mContext = c;
this.id = id;
this.item = itemname;
this.description = itemdesc;
this.quantity = itemquantity;
}

public int getCount() {
// TODO Auto-generated method stub
return id.size();
}

public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

public View getView(int pos, View child, ViewGroup parent) {
Holder mHolder;
LayoutInflater layoutInflater;
if (child == null) {
layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutInflater.inflate(R.layout.listcell, null);
mHolder = new Holder();
mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
mHolder.txt_N = (TextView) child.findViewById(R.id.txt_name);
mHolder.txt_D = (TextView) child.findViewById(R.id.txt_desc);
mHolder.txt_Q = (TextView) child.findViewById(R.id.txt_quantity);
child.setTag(mHolder);
} else {
mHolder = (Holder) child.getTag();
}
mHolder.txt_id.setText(id.get(pos));
mHolder.txt_N.setText(item.get(pos));
mHolder.txt_D.setText(description.get(pos));
mHolder.txt_Q.setText(quantity.get(pos));

return child;
}

public class Holder {
TextView txt_id;
TextView txt_N;
TextView txt_D;
TextView txt_Q;
}

}

它如何发布在我的 ListView 上?帮我。多谢。这是我的代码

HomePage.java

package com.example.aoutsu;

import java.util.ArrayList;




import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

public class HomePage extends Activity {
private DbHelper mHelper;
private SQLiteDatabase dataBase;

private ArrayList<String> item_id = new ArrayList<String>();
private ArrayList<String> name_item = new ArrayList<String>();
private ArrayList<String> desc_item = new ArrayList<String>();
private ArrayList<String> quantity = new ArrayList<String>();

private ListView itemList;
private AlertDialog.Builder build;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
itemList = (ListView) findViewById(R.id.listView);

mHelper = new DbHelper(this);
findViewById(R.id.addBtn).setOnClickListener(new OnClickListener() {

public void onClick(View v) {

Intent i = new Intent(getApplicationContext(),
AddNewItem.class);
i.putExtra("update", false);
startActivity(i);

}
});

//click to update data
itemList.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {

Intent i = new Intent(getApplicationContext(),
AddNewItem.class);
i.putExtra("ItemName", name_item.get(arg2));
i.putExtra("ItemDesc", desc_item.get(arg2));
i.putExtra("Quantity", quantity.get(arg2));
i.putExtra("ID", item_id.get(arg2));
i.putExtra("update", true);
startActivity(i);
}
});



//long click to delete data
itemList.setOnItemLongClickListener(new OnItemLongClickListener() {

public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
final int arg2, long arg3) {

build = new AlertDialog.Builder(HomePage.this);
build.setTitle("Delete " + name_item.get(arg2) + " "
+ desc_item.get(arg2) + quantity.get(arg2));
build.setMessage("Do you want to delete ?");
build.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,
int which) {

Toast.makeText(
getApplicationContext(),
name_item.get(arg2) + " "
+ desc_item.get(arg2) +" " + quantity.get(arg2)
+ " is deleted.", 3000).show();

dataBase.delete(
DbHelper.TABLE_NAME,
DbHelper.KEY_ID + "="
+ item_id.get(arg2), null);
displayData();
dialog.cancel();
}
});

build.setNegativeButton("No",
new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,
int which) {
dialog.cancel();
}
});
AlertDialog alert = build.create();
alert.show();

return true;
}
});
}

@Override
protected void onResume() {
displayData();
super.onResume();
}

/**
* displays data from SQLite
*/
private void displayData() {
dataBase = mHelper.getWritableDatabase();
Cursor mCursor = dataBase.rawQuery("SELECT * FROM "
+ DbHelper.TABLE_NAME, null);

item_id.clear();
name_item.clear();
desc_item.clear();
quantity.clear();
if (mCursor.moveToFirst()) {
do {
item_id.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ID)));
name_item.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ITEMNAME)));
desc_item.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ITEMDESC)));
quantity.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ITEMQUANTITY)));

} while (mCursor.moveToNext());
}
DisplayAdapter disadpt = new DisplayAdapter(HomePage.this,item_id, name_item, desc_item,quantity);
itemList.setAdapter(disadpt);
mCursor.close();
}





}

最佳答案

您是否先卸载了应用程序?似乎没有名为“itemquantity”的列,当您创建数据库并尝试添加新列时会发生这种情况,尝试完全删除该应用程序,然后再次运行该应用程序,看看这是否有帮助。

关于java - 数据 未存储数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21220583/

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