Android数据库提取查询

时间:2013-11-12 06:50:06

标签: android android-layout android-sqlite

private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "jobDiagnosis";
private static final int DATABASE_VERSION = 2;
Cursor cursor = null;
private static final String TABLE_DATA = "create table Job_Saved (" +
        "Id text not null," +
        "Title text not null," +
        "Location text not null," +
        "State text not null," +
        "Company text not null," +
        "Description text not null," +
        "Status text not null," +
        "Username text not null," +
        "Password text not null)";
private final Context context;
private DatabaseHelper dbHelper;
private SQLiteDatabase db;
private static DBAdapter instance;

private DBAdapter(Context c) {
    System.out.println("Constructor of DB Adapter...");
    this.context = c;
    System.out.println("Creating the object of db helper class..");
    try
    {
    dbHelper = new DatabaseHelper(context);
    dbHelper.getWritableDatabase();
    }
    catch (Exception e) {
        // TODO: handle exception
        Log.d("err", ""+e);
    }
}

public static DBAdapter getInstance(Context C) {
    if (null == instance) {
        instance = new DBAdapter(C);
    }
    return instance;
}

public void openReadableDatabase() throws SQLException {
    db = dbHelper.getReadableDatabase();
}

public void openWritableDatabase() throws SQLException {
    db = dbHelper.getWritableDatabase();

}

public void close() {
    dbHelper.close();   
}   
public long DeleteLocation(String name)
{
    return db.delete("Job_Saved", null, null);
}
public long insertlocation(String Id,String Title,String Location,String State,String Company,String Description,String value, String Username,String Password) {
    //DatabaseHelper d=new DatabaseHelper(DBAdapter.this);
    ContentValues initialValues = new ContentValues();
    initialValues.put("Id", Id);
    initialValues.put("Title", Title);
    initialValues.put("Location", Location);
    initialValues.put("State", State);
    initialValues.put("Company",Company);
    initialValues.put("Description", Description);
    initialValues.put("Status", value);
    initialValues.put("Username", Username);
    initialValues.put("Password", Password);
    return db.insert("Job_Saved", null, initialValues);
}
public ArrayList<Data> getAllData(){
    ArrayList<Data> arr = new ArrayList<Data>();
    Cursor c = db.query("Job_Saved", null, null, null, null, null, null);   
    if(c.getCount()>0){
        while(c.moveToNext()){
            Data f = new Data();
            f.setid(c.getString(0));
            f.setbusinessname(c.getString(1));
            f.setcityname(c.getString(2));
            f.setstatename(c.getString(3));
            f.setcompanyname(c.getString(4));
            f.setDesc(c.getString(5));
            f.setStatus(c.getString(6));
            f.setUser(c.getString(7));
            //f.setCharging(c.getString(8));
            arr.add(f);
        }

    }
    c.close();
    return arr;
}
public void updateDownload(String id  , String status)
{
    ContentValues initialValues = new ContentValues();
    initialValues.put("status", status);
    db.update("Likes", initialValues, "id=?", new String[] {id});
}
public String getIdStatus(String id){
    String a="";
    Cursor c =  db.query("Likes", null, "id='" + id + "'", null, null, null,
            null);      
    if(c.getCount()>0){
        while(c.moveToNext())
        {
         a=c.getString(1);
        }

    }
    return a;
}

private class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);

        System.out.println("Constructor of DB Helper Class...");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        System.out.println("on create of database helper class..");

        try {
            // Creating Database...........................................
            db.execSQL(TABLE_DATA);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onOpen(SQLiteDatabase db) {
        // DBAdapter.this.db=db;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Log.w(TAG, "Upgrading database from version " + oldVersion
        // + " to "
        // + newVersion + ", which will destroy all old data");
        // db.execSQL("DROP TABLE IF EXISTS users");
        // onCreate(db);
    }
}

public String getFolderkeywordname(String string) {
    String index = "";
    Cursor c =  db.query("folders", new String[] { "keyword" }, "name='" + string + "'", null, null, null,
            null);      
    if(c.getCount()>0){
        while(c.moveToNext()){
            System.out.println("folder id is : " + index);
            index =  c.getString(0);

        }
    }
    c.close();
    return index;
}

}

这是我的数据库适配器类。我还定义了一个getter和setter类,但我不知道从数据库获取值所以请帮助我。

我还想使用提取和删除查询

感谢任何帮助。提前谢谢。

0 个答案:

没有答案