如何在android中的另一个活动中更新表

时间:2013-12-05 08:01:52

标签: android sqlite

我正在一个名为 Customer_page.java 的活动的表中创建和插入数据,代码如下:

db= openOrCreateDatabase("dsr", MODE_PRIVATE, null);
        db.execSQL("create table if not exists customer_form" +
                "(id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "sales_person TEXT,current_date Text,contact_person TEXT, contact_number TEXT, distance TEXT, description TEXT," +
                "location TEXT, type_of_meeting TEXT,interest TEXT, time_in TEXT, time_out TEXT, flag INTEGER)"
                );

并插入如下数据:

db.execSQL("insert into customer_form(sales_person,current_date,contact_person,contact_number,distance,description,"+
                        "location,type_of_meeting,interest,time_in,time_out,flag) values" +
                        "('null',date('now'),'"+contact_person+"','"+contact_number+"','"+distance+"','"+description+"','"+location+"','"+type_of_meeting+"','"+interested+"',"+
                                "'"+time_in+"','"+time_out+"',0)");

现在我正在更新 Welcome.java 活动中的customer_form表,代码如下:

db=openOrCreateDatabase("dsr", MODE_PRIVATE, null);
db.execSQL("update customer_form set sales_person='"+email_id+"' where sales_person='null'");
    db.close();

但是这段代码没有更新表而且没有给出任何错误。如何在 Welcome.java 活动中更新customer_form表?

2 个答案:

答案 0 :(得分:2)

我使用下面给出的代码:

首先我创建了一个新类 Database_Update.java

package com.example.hello;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Database_Update extends  SQLiteOpenHelper{

private static final String DB_NAME="dsr";
private static final int DB_VER=1;      

public Database_Update(Context context) {
    super(context,DB_NAME, null, DB_VER);
}

@Override
public void onCreate(SQLiteDatabase database) {     
}

@Override
public void onUpgrade(SQLiteDatabase database, int arg1, int arg2) {         
    onCreate(database);
 }
}

然后在我的新活动中访问表以进行更新:

Database_Update du=new Database_Update(this);
    SQLiteDatabase sd=du.getReadableDatabase();
    ContentValues value=new ContentValues();
    value.put("sales_person", email_id);
    int updated=sd.update("customer_form", value, "sales_person='no_value'", null);

答案 1 :(得分:0)

您可以使用"update customer_form set sales_person='"+email_id+"' where sales_person IS NULL OR sales_person=''"