如何在sqlite Android中使用select和where子句?

时间:2014-04-03 19:45:58

标签: android sql sqlite

我有以下代码。如何使用select从列表中获取特定值? 就像我想使用select * from table_name where Name=Sam;并在字符串中获取结果值一样。

如何根据以下示例在SQLITE中执行此操作?

package com.example.sqlitedemo;
public class MainActivity extends Activity {


LinearLayout Linear;
SQLiteDatabase mydb;
private static String DBNAME = "PERSONS.db";    // THIS IS THE SQLITE      
DATABASE FILE NAME.
private static String TABLE = "MY_TABLE";       // THIS IS <span id="IL_AD9" class="IL_AD">THE TABLE</span> NAME
TextView textView1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView1 = (TextView)findViewById(R.id.textView1);

     Toast.makeText(getApplicationContext(), "Creating table.", Toast.LENGTH_SHORT).show();

    dropTable();        // DROPPING THE TABLE.
    createTable();

    insertIntoTable();
    selectTable();

}
public void showTableValues(){
    try{
        int rowid=1;
        mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);

       // Cursor allrows  = mydb.rawQuery("SELECT * FROM "+  TABLE, null);
    }
    finally{

    }
}
// CREATE TABLE IF NOT EXISTS 
public void createTable(){
    try{
    mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
    mydb.execSQL("CREATE TABLE IF  NOT EXISTS "+ TABLE +" (ID INTEGER PRIMARY KEY, NAME TEXT, PLACE TEXT);");
    mydb.close();
    }catch(Exception e){
        Toast.makeText(getApplicationContext(), "Error in creating table", Toast.LENGTH_LONG);
    }
}
// THIS FUNCTION INSERTS DATA TO THE DATABASE
public void insertIntoTable(){
    try{
        mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
        mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE) VALUES('CODERZHEAVEN','GREAT INDIA')");
        mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE) VALUES('ANTHONY','USA')");
        mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE) VALUES('SHUING','JAPAN')");
        mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE) VALUES('JAMES','INDIA')");
        mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE) VALUES('SOORYA','INDIA')");
        mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE) VALUES('MALIK','INDIA')");
        mydb.execSQL("INSERT INTO " + TABLE + "(NAME, PLACE) VALUES('myname','America')");
        mydb.close();
    }catch(Exception e){
        Toast.makeText(getApplicationContext(), "Error in inserting into table", Toast.LENGTH_LONG);
    }
}
public void selectTable(){
    try{
    mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
    Cursor c = mydb.rawQuery("select * from TABLE where NAME=JAMES", null);

    String name = c.getString(0);
    String place = c.getString(1);
    textView1.setText(name);
    mydb.close();
    }catch(Exception e){
        Toast.makeText(getApplicationContext(), "Error selecting", Toast.LENGTH_LONG);
    }

}

public void dropTable(){
    try{
        mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
        mydb.execSQL("DROP TABLE " + TABLE);

        mydb.close();
    }catch(Exception e){
        Toast.makeText(getApplicationContext(), "Error encountered while dropping.", Toast.LENGTH_LONG);
    }
}
}

2 个答案:

答案 0 :(得分:1)

您的变量mydb

Cursor c = mydb.rawQuery("select * from MY_TABLE where NAME=MALIK");
String name = c.getString(c.getColumnIndex("NAME"));
String place = c.getString(c.getColumnIndex("PLACE"));

现在,根据您在代码段中提供的数据,您可以根据自己的需要访问两列NamePlace

答案 1 :(得分:0)

使用此查询

SQLiteDatabase db = this.getReadableDatabase();

    if (cursorr.moveToFirst()) 
   {

        do {

            // your code like get columns

            }
         while (cursorr.moveToNext());
    }
}

Cursor cursorr = db.rawQuery(&#34; select * from&#34; + DATABASE_TABLE_EHS +&#34; where&#34; + TASK_ID +&#34; =&#39;&#34; + taskid +&#34;&#39;&#34;,null);

if(root){
if(root->left !=NULL || root->right !=NULL){
    if(root->left)
        top_view(root->left);

     cout<<root->data<<" ";

     if(root->right)
        top_view(root->right);

}}