rawquery选择作为数据库输入

时间:2011-10-12 07:25:28

标签: java android sqlite

我想知道如何获取所选rawquery的值

Cursor data = dbObject.rawQuery("SELECT _id FROM COMAPNY WHERE _id LIKE ?",
                 new String[] { "%" + val2 + "%" });

作为sqlite数据库输入。我想选择数字(val2),然后将该数字添加到handle.KEY_ID2列。它有可能,怎么做?

完整代码

数据库

package test.com;

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

public class handle extends SQLiteOpenHelper {

     public static final String DB_NAME = "SHARE_DB";
     public static final String DB_COMPANYTABLE = "COMAPNY";
     public static final String DB_SHARETABLE = "SHARE";
     public static final int DB_VERSION = 1;
     public static final String KEY_ID = "_id";
     public static final String KEY_ID2 = "_id2";
     public static final String ROW_ID = "_id";
     public static final String COMPANY_NAME = "Name";
     public static final String COMPANY_SHAREP = "Price";
     public static final String COMPANY_SHAREV = "Volume";

    public handle(Context context, String name, CursorFactory factory,
            int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL(SCRIPT_CREATE_DATABASE);
        db.execSQL(SCRIPT_CREATE_DATABASE2);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DB_COMPANYTABLE);
        db.execSQL("DROP TABLE IF EXISTS " + DB_SHARETABLE);
        onCreate(db);
    }

    private static final String SCRIPT_CREATE_DATABASE = "create table " + DB_COMPANYTABLE + " (" 
                +KEY_ID + " integer primary key autoincrement, " 
                + COMPANY_NAME + " text not null);";

private static final String SCRIPT_CREATE_DATABASE2 = "create table " + DB_SHARETABLE + " (" 
                + ROW_ID + " integer primary key autoincrement, " 
                + KEY_ID2 + " integer, " 
                + COMPANY_SHAREP + " text not null, " 
                + COMPANY_SHAREV + " text not null);";
}

活性1

private ListView.OnItemClickListener listContentOnItemClickListener
   = new ListView.OnItemClickListener(){

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,
    long id) {
Company comid = new Company();
      Cursor cursor = (Cursor) parent.getItemAtPosition(position);
      int item_id = cursor.getInt(cursor.getColumnIndex(handle.KEY_ID));

               String item = String.valueOf(item_id);

           comid.setCompanyID(item);    

           startActivity(new Intent("android.intent.action.next"));
     }};

活性2

    Company obj1 = new Company();
    String val1 = obj1.getCompanyID();

protected void saveRecord() {
    // TODO Auto-generated method stub
    conValue.put(handle.COMPANY_SHAREP, two.getText().toString());
    conValue.put(handle.COMPANY_SHAREV, three.getText().toString());
    conValue.put(handle.KEY_ID2, four.getText().toString());
    dbObject.insert(handle.DB_SHARETABLE, null, conValue);
}

至于现在我必须输入KEY_ID2,但我想要的是......当用户点击ACTIVITY1列表时...它将行id发送为“val1”&amp;我希望在ACTIVITY2按钮上输入该值,然后单击

1 个答案:

答案 0 :(得分:0)

没有直接的方法。你需要:

  1. 查询数据库以获取光标
  2. 将光标移动到第一个元素
  3. 从光标中获取String / Int / Whatever,
  4. 使用插入来插入此值。
  5. 但我在这里看到一个非常基本的问题:你的数据库布局。它永远不会依赖冗余保存的数据!

    请查看您的数据库并检查您是否可以使用关系在两个值之间建立连接。