SQL的查询操作。是用SQLiteDatabase提供的query()方法,对数据进行查询。这个方法的参数比较复杂,最少的都有7个参数。
query(table,columns, selection, selectionArgs, groupBy, having, orderBy, limit)
table:要查询的数据表
columns:需要查询的字段,也就是列名
selection : 查询的子条件,相当于select语句中的where部分,在条件子句允许使用占位符“?”
selectionArges : 对应着selection的占位符的值,要一一对应
groupBy : 相当于select语句的groupby后面的部分
having : 相当于select语句的having后面的部分
orderBy : 相当于select语句的orderBy后面的部分 ,如: personid desc, age asc;
limit : 指定获取数据的条数(偏移量),类似于select语句的limit关键字
columns:要查询出来的列名。相当于select语句select关键字后面的部分。
调用query()方法后会返回Cursor对象,查询到的所有数据都从这个对象中取出。
以下为代码例子:
这里,我把查询到的数据使用Intent传到另一个页面:
package com.example.sqlapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private Button create_database,add_data,update_data,delete_data,query_data; private MyDatabaseHelper dbHelper; private TextView txt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2); create_database = (Button)findViewById(R.id.create_database); add_data =(Button)findViewById(R.id.add_data); update_data = (Button)findViewById(R.id.update_data); delete_data = (Button)findViewById(R.id.delete_data); query_data = (Button)findViewById(R.id.query_data); txt = (TextView)findViewById(R.id.txt); create_database.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dbHelper.getReadableDatabase(); } }); add_data.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name","La la land"); values.put("author","Jane Zhong"); values.put("pages",88); values.put("price",88); db.insert("Book",null,values); values.clear(); //第二组 values.put("name","Dong success"); values.put("author","Jane Zhong"); values.put("pages",88); values.put("price",88); db.insert("Book",null,values); } }); update_data.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SQLiteDatabase db = dbHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("price",10); db.update("Book",values,"name = ?",new String[] {"Dong success"}); } }); delete_data.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SQLiteDatabase db = dbHelper.getWritableDatabase(); db.delete("Book","pages > ?",new String[] {"500"}); } }); query_data.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SQLiteDatabase db = dbHelper.getReadableDatabase(); Cursor cursor = db.query("Book",null,null,null,null,null,null); if(cursor.moveToFirst()){ do{ //遍历cursor对象,并取出打印 String name = cursor.getString(cursor.getColumnIndex("name")); String author = cursor.getString(cursor.getColumnIndex("author")); int pages = cursor.getInt(cursor.getColumnIndex("pages")); double price = cursor.getColumnIndex("price"); Intent intent = new Intent(MainActivity.this,SecondActivity.class); String data = "Name of book:"+name+",\nauthor:"+author+",\npages:"+pages+",\nprice:"+price; intent.putExtra("data",data); startActivity(intent); }while(cursor.moveToNext()); } cursor.close(); } }); } }
接下来是接收的页面:
package com.example.sqlapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class SecondActivity extends AppCompatActivity { private TextView txt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); txt = findViewById(R.id.txt); Intent intent = getIntent(); String data = intent.getStringExtra("data"); txt.setText(data); } }