My problem is how to find the related records from two or three talbes created in one database when type the EditText"start" and "end" then click "Busfind".
When I create a sqlite database with 3 tables with another sqlite tool,
later,, I push it into the emulator,and it works well, I mean the data can be read and written by some Cursors like
- Code: Select all
Cursor cst=myDB.rawQuery("SELECT node.n_name,ln.l_id" +
" FROM " + MY_DATABASE_TABLE4 +", " + MY_DATABASE_TABLE3 +
" WHERE node.n_name = " +
"'" + busStart +"'", null);
this show me a list lines which contains the entry"start",look at the image below;

or which Cursor shall I use?
- Code: Select all
// Cursor ced=myDB.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
// Cursor ced=myDB.query(table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
// Cursor ced=myDB.query(distinct, table, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
// Cursor ced=myDB.queryWithFactory(cursorFactory, distinct, table, columns, selection, selectionArgs, groupBy, having, orderBy, limit)
// Cursor ced=myDB.rawQueryWithFactory(cursorFactory, sql, selectionArgs, editTable)
please Someone show me the way to this problem, Thank you very much.
the code is,
Using java Syntax Highlighting
- package child.bf;
- import java.util.ArrayList;
- import android.app.ListActivity;
- import android.content.Intent;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.text.GetChars;
- import android.widget.ArrayAdapter;
- import android.widget.EditText;
- public class DataBaseWork extends ListActivity {
- private final String MY_DATABASE_NAME = "bus.db";
- private final String MY_DATABASE_TABLE2 = "line";
- private final String MY_DATABASE_TABLE3 = "node";
- private final String MY_DATABASE_TABLE4 = "ln";
- public static final String BUS_START = "startInput";
- public static final String BUS_END = "endInput";
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- Intent intent = getIntent();
- String busStart = intent.getStringExtra(DataBaseWork.BUS_START);
- String busEnd = intent.getStringExtra(DataBaseWork.BUS_START);
- // EditText et = new EditText(this);
- // et.setSelection(et.getText().length());
- /* Will hold the 'Output' we want to display at the end. */
- ArrayList<String> results = new ArrayList<String>();
- SQLiteDatabase myDB = null;
- try {
- /* Create the Database (no Errors if it already exists) */
- myDB = this.openOrCreateDatabase(MY_DATABASE_NAME, MODE_PRIVATE, null);
- /* Create a Table in the Database. */
- //table line
- myDB.execSQL("CREATE TABLE IF NOT EXISTS "
- + MY_DATABASE_TABLE2
- + " (l_id INT(3), l_name VARCHAR,"
- + " n_ids VARCHAR);");
- //talbe line_node as ln
- myDB.execSQL("CREATE TABLE IF NOT EXISTS "
- + MY_DATABASE_TABLE4
- + " (l_id INT(3), "
- + " n_id INT(3));");
- //talbe node
- myDB.execSQL("CREATE TABLE IF NOT EXISTS "
- + MY_DATABASE_TABLE3
- + " (n_id INT(3),n_name VARCHAR, "
- + " n_lat INT(4),n_lon INT(4),"
- + " l_ids VARCHAR );");
- /* Query for some results with Selection and Projection. */
- Cursor cst=myDB.rawQuery("SELECT node.n_name,ln.l_id" +
- " FROM " + MY_DATABASE_TABLE4 +", " + MY_DATABASE_TABLE3 +
- " WHERE node.n_name = " +
- "'" + busStart +"'", null);
- /* Get the indices of the Columns we will need */
- int l_idColume=cst.getColumnIndexOrThrow("l_id");
- int n_nameColume=cst.getColumnIndexOrThrow("n_name");
- /* Check if our result was valid. */
- cst.moveToFirst();
- if (cst != null) {
- /* Check if at least one Result was returned. */
- if (cst.isFirst()) {
- int i = 0;
- /* Loop through all Results */
- do {
- i++;
- /* Retrieve the values of the Entry
- * the Cursor is pointing to. */
- String nname = cst.getString(n_nameColume);
- int lid=cst.getInt(l_idColume);
- // int nid=cst.getInt(n_idColume);
- /* We can also receive the Name
- * of a Column by its Index.
- * Makes no sense, as we already
- * know the Name, but just to show we can */
- // String ageColumName = c.getColumnName(ageColumn);
- /* Add current Entry to results. */
- results.add("" + i + "(line_id: " + lid + "node_name;" +nname);
- } while(cst.moveToNext());
- }
- }
- } finally {
- if (myDB != null)
- myDB.close();
- }
- this.setListAdapter(new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, results));
- }
- }
Parsed in 0.053 seconds, using GeSHi 1.0.8.4

