Android数据库无法正确保存

时间:2015-08-12 22:54:47

标签: java android mysql

我创建了一个非常简单的Android应用程序,它询问用户的详细信息,如名称,数字等,并将其保存在数据库中,然后在不同的屏幕上打印。但是我对数据库有一些问题,似乎不是覆盖数据库中的行,而是在同一行中添加文本,例如我有一个名称的编辑文本我可以输入:" James&# 34;然后保存它。然后当我回去覆盖例如更改名称的详细信息,例如" Ben"而不是覆盖行似乎添加在同一行,所以它的喜欢 " JimmyBen&#34 ;.任何想法我怎么可能改变这个?

数据库代码:

package com.example.androidsimpledbapp1;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;

public class MyDBHandler extends SQLiteOpenHelper {

/*
 * Class for Working with DB 
 */

//Update each time DB structure changes e.g. adding new property
private static final int DATABASE_VERSION =1;
//DB Name
private static final String DATABASE_NAME = "details.db";
//Table name
public static final String  TABLE_PRODUCTS = "products";
//DB Columns 
public static final String  COLUMN_ID = "_Id";
public static final String  COLUMN_PERSONNAME  = "firstName";
public static final String  COLUMN_PERSONBLOOD  = "bloodType";
public static final String  COLUMN_PERSONCONTACT  = "contactName";
public static final String  COLUMN_PERSONNUMBER  = "phoneNumber";
public static final String  COLUMN_PERSONRELATION = "relationship";

//Constructor
/*
 * Passing information to super class in SQL
 * Context is background information 
 * name of db 
 * Database version
 */
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

/*
 * What to do first time when you create DB
 * Creates the table the very first time
 * (non-Javadoc)
 * @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
 * Remember to use Commas as shown below
 */
@Override
public void onCreate(SQLiteDatabase db){
    String query = "CREATE TABLE "+ TABLE_PRODUCTS + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+
            COLUMN_PERSONNAME + " TEXT, "+
            COLUMN_PERSONBLOOD + " TEXT, "+
            COLUMN_PERSONCONTACT + " TEXT, "+
            COLUMN_PERSONNUMBER + " TEXT, " +
            COLUMN_PERSONRELATION + " TEXT " +
            ");";
    //Execute the query
    db.execSQL(query);
}

/*
 * If ever upgrading DB call this method
 * (non-Javadoc)
 * @see android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite.SQLiteDatabase, int, int)
 */
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
    //Delete the current table
    db.execSQL("DROP TABLE IF EXISTS" + TABLE_PRODUCTS);
    //create new table 
    onCreate(db);
}

//Add new row to the database
public void addProduct(Details details){
    //Built in class - set values for different columns 
    //Makes inserting rows quick and easy
    ContentValues values = new ContentValues();
    values.put(COLUMN_PERSONNAME, details.get_firstName());
    values.put(COLUMN_PERSONBLOOD, details.get_bloodType());
    values.put(COLUMN_PERSONCONTACT, details.get_contactName());
    values.put(COLUMN_PERSONNUMBER, details.get_phoneNumber());
    values.put(COLUMN_PERSONRELATION, details.get_relationship());
    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_PRODUCTS, null, values);
    db.close();
}

/*Table was deleted*/
public void deleteProducts(){
    SQLiteDatabase db = getWritableDatabase();      
    db.delete(TABLE_PRODUCTS, null, null);
}

//Take DB and Convert to String 
public String databaseToString(){
    String dbString = "";
    SQLiteDatabase db = getWritableDatabase();
    //Every Column and row
    String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";

    //Cursor points to a location in your results
    //First row point here, second row point here

    Cursor c = db.rawQuery(query, null);
    c.moveToFirst();

    while(!c.isAfterLast()){
        //Extracts first name and adds to string
        if(c.getString(c.getColumnIndex("firstName"))!=null){
            dbString += c.getString(c.getColumnIndex("firstName"));
            c.moveToNext();
            /*
             * Displaying all other columns 
             */
        }
    }
    db.close();
    return dbString;
}
  }

1 个答案:

答案 0 :(得分:0)

您的addProduct方法使用db.insert,它始终会创建一个新行。还有db.update用于修改行。

您需要确保指定正确的WHERE子句,以便修改正确的行。对于使用主键的单行优先,通常是来自db.insert的响应。