Android 韩元';t将值插入数据库| won';跑不动
我最近做了一些小改动,似乎看不出是什么错误导致了这一切。非常感谢您的帮助 主要 DB处理程序-我的大部分更改都是在这里进行的,试图添加数量(quan)Android 韩元';t将值插入数据库| won';跑不动,android,sqlite,Android,Sqlite,我最近做了一些小改动,似乎看不出是什么错误导致了这一切。非常感谢您的帮助 主要 DB处理程序-我的大部分更改都是在这里进行的,试图添加数量(quan) package com.androidhive.androidsqlite; 导入java.util.ArrayList; 导入java.util.List; 导入android.content.ContentValues; 导入android.content.Context; 导入android.database.Cursor; 导入andro
package com.androidhive.androidsqlite;
导入java.util.ArrayList;
导入java.util.List;
导入android.content.ContentValues;
导入android.content.Context;
导入android.database.Cursor;
导入android.database.sqlite.SQLiteDatabase;
导入android.database.sqlite.SQLiteOpenHelper;
公共类DatabaseHandler扩展了SQLiteOpenHelper{
//所有静态变量
//数据库版本
私有静态最终int数据库_VERSION=1;
//数据库名称
私有静态最终字符串数据库\u NAME=“comstuff”;
//联系人表名称
专用静态最终字符串表\u CONTACTS=“coms”;
//联系人表列名称
私有静态最终字符串密钥\u ID=“ID”;
私有静态最终字符串键\u NAME=“NAME”;
私有静态最终字符串键\u PRICE=“PRICE”;
私有静态最终字符串密钥\u QUAN=“数量”;
公共数据库处理程序(上下文){
super(上下文、数据库名称、null、数据库版本);
}
//创建表
@凌驾
public void onCreate(SQLiteDatabase db){
字符串CREATE_CONTACTS_TABLE=“CREATE TABLE”+TABLE_CONTACTS+”(“
+键ID+“整数主键”+“键名称+”文本
+键价+“文本”+“键权+”文本“+”;
execSQL(创建联系人表);
}
//升级数据库
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
//删除旧表(如果存在)
db.execSQL(“如果存在删除表”+表_联系人);
//再次创建表
onCreate(db);
}
/**
*所有CRUD(创建、读取、更新、删除)操作
*/
//添加新联系人
无效添加联系人(通信联系人){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues=新的ContentValues();
value.put(KEY_NAME,contact.getName());//contact NAME
value.put(KEY_PRICE,contact.getPhoneNumber());//联系电话
value.put(KEY_QUAN,contact.getQuan());//联系电话
//插入行
db.插入(表_触点,空,值);
db.close();//关闭数据库连接
}
//获得单一联系
Coms getContact(内部id){
SQLiteDatabase db=this.getReadableDatabase();
Cursor Cursor=db.query(TABLE_CONTACTS,新字符串[]{KEY_ID,
密钥名称,密钥价格,密钥权,密钥ID+“=?”,
新字符串[]{String.valueOf(id)},null,null,null,null);
如果(光标!=null)
cursor.moveToFirst();
Coms contact=new Coms(Integer.parseInt(cursor.getString(0)),
cursor.getString(1)、cursor.getString(2)、cursor.getString(3));
//回接
回接;
}
Coms getName(内部id){
SQLiteDatabase db=this.getReadableDatabase();
Cursor Cursor=db.query(表_联系人,新字符串[]){
密钥名称,密钥价格,密钥权,密钥ID+“=?”,
新字符串[]{String.valueOf(id)},null,null,null,null);
如果(光标!=null)
cursor.moveToFirst();
Coms contact=new Coms(Integer.parseInt(cursor.getString(0)),
cursor.getString(1)、cursor.getString(2)、cursor.getString(3));
//回接
回接;
}
//获取所有联系人
公共列表getAllContacts(){
List contactList=new ArrayList();
//选择所有查询
String selectQuery=“SELECT*FROM”+表格\联系人;
SQLiteDatabase db=this.getWritableDatabase();
Cursor Cursor=db.rawQuery(selectQuery,null);
//循环遍历所有行并添加到列表
if(cursor.moveToFirst()){
做{
通信系统触点=新通信系统();
setID(Integer.parseInt(cursor.getString(0));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
//当该值从2更改为3时,会导致错误
contact.setQuan(cursor.getString(3));
//将联系人添加到列表中
联系人列表。添加(联系人);
}while(cursor.moveToNext());
}
//返回联系人列表
返回联系人列表;
}
//更新单个联系人
public int updateContact(通信系统联系人){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues=新的ContentValues();
value.put(KEY_NAME,contact.getName());
value.put(KEY_PRICE,contact.getPhoneNumber());
value.put(KEY_QUAN,contact.getQuan());
//更新行
返回db.update(表联系人、值、键ID+“=?”,
新字符串[]{String.valueOf(contact.getID())};
}
//删除单个联系人
公共联系人(通信联系人){
SQLiteDatabase db=this.getWritableDatabase();
db.delete(表触点,键ID+“=?”,
新字符串[]{String.valueOf(contact.getID())};
db.close();
}
//获取联系人计数
public int getcontactscont(){
String countQuery=“SELECT*FROM”+表\u联系人;
SQLiteDatabase db=this.getReadableDatabase();
Cursor Cursor=db.rawQuery(countQuery,null);
cursor.close();
//返回计数
返回cursor.getCount();
}
}
数据库表仅在第一次运行时创建。如果数据库已创建,则不会再次调用onCreate。因此,如果向表中添加一个新列,并插入涉及该新列的记录,则会出现错误。清除数据或卸载应用程序,然后
package com.androidhive.androidsqlite;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class AndroidAss2SQL extends Activity {
/** Called when the activity is first created. */
TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatabaseHandler db = new DatabaseHandler(this);
/**
* CRUD Operations
* */
// Inserting Contacts
Log.d("Insert: ", "Inserting ..");
db.addContact(new Coms("Gold", "100.00", "0"));
db.addContact(new Coms("Silver", "300.00", "3"));
db.addContact(new Coms("Wheat", "500.00", "4"));
db.addContact(new Coms("Sugar", "750.00", "0"));
db.addContact(new Coms("Coffee", "433.00", "9"));
// Reading all contacts
Log.d("Reading: ", "Reading all coms..");
List<Coms> contacts = db.getAllContacts();
for (Coms cn : contacts) {
String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber() + " ,Quantity: " + cn.getQuan();
// Writing Contacts to log
Log.d("Name: ", log);
}
Coms rando = db.getContact(1);
text = (TextView) findViewById(R.id.textView2);
text.setText(String.valueOf(rando));
Coms rando1 = db.getContact(2);
text = (TextView) findViewById(R.id.textView4);
text.setText(String.valueOf(rando1));
Coms rando2 = db.getContact(3);
text = (TextView) findViewById(R.id.textView6);
text.setText(String.valueOf(rando2));
Coms rando3 = db.getContact(4);
text = (TextView) findViewById(R.id.textView8);
text.setText(String.valueOf(rando3));
Coms rando4 = db.getContact(5);
text = (TextView) findViewById(R.id.textView10);
text.setText(String.valueOf(rando4));
}
}
package com.androidhive.androidsqlite;
public class Coms {
//private variables
int _id;
String _name;
String _price;
String _quan;
// Empty constructor
public Coms(){
}
// constructor
public Coms(int id, String name, String price){
this._id = id;
this._name = name;
this._price = price;
}
// constructor
public Coms(String name, String price){
this._name = name;
this._price = price;
}
// constructor
public Coms(String name){
this._name = name;
}
public Coms(String name, String price, String quan){
this._name = name;
this._price = price;
this._quan = quan;
}
public Coms(int id, String name, String price, String quan){
this._id = id;
this._name = name;
this._price = price;
this._quan = quan;
}
@Override
public String toString(){
return this._price;
}
// getting ID
public int getID(){
return this._id;
}
// setting id
public void setID(int id){
this._id = id;
}
// getting name
public String getName(){
return this._name;
}
// setting name
public void setName(String name){
this._name = name;
}
// getting phone number
public String getPhoneNumber(){
return this._price;
}
// setting phone number
public void setPhoneNumber(String phone_number){
this._price = phone_number;
}
// getting name
public String getQuan(){
return this._quan;
}
// setting name
public void setQuan(String name){
this._quan = name;
}
}
package com.androidhive.androidsqlite;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "comstuff";
// Contacts table name
private static final String TABLE_CONTACTS = "coms";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PRICE = "price";
private static final String KEY_QUAN = "quantity";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_PRICE + " TEXT," + KEY_QUAN + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
void addContact(Coms contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName()); // Contact Name
values.put(KEY_PRICE, contact.getPhoneNumber()); // Contact Phone
values.put(KEY_QUAN, contact.getQuan()); // Contact Phone
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}
// Getting single contact
Coms getContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME, KEY_PRICE, KEY_QUAN }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Coms contact = new Coms(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), cursor.getString(3));
// return contact
return contact;
}
Coms getName(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] {
KEY_NAME, KEY_PRICE, KEY_QUAN }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Coms contact = new Coms(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2), cursor.getString(3));
// return contact
return contact;
}
// Getting All Contacts
public List<Coms> getAllContacts() {
List<Coms> contactList = new ArrayList<Coms>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Coms contact = new Coms();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
//when this is changed from 2 to 3 it causes an error
contact.setQuan(cursor.getString(3));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
// Updating single contact
public int updateContact(Coms contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, contact.getName());
values.put(KEY_PRICE, contact.getPhoneNumber());
values.put(KEY_QUAN, contact.getQuan());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}
// Deleting single contact
public void deleteContact(Coms contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
db.close();
}
// Getting contacts Count
public int getContactsCount() {
String countQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}