如何在SQLite数据库上使用游标

时间:2012-07-14 15:36:44

标签: android database sqlite

我正在按照教程制作数据库游标,但是我遇到了一个问题。当我编写下面的代码时,我没有选择在数据库名称之后添加查询:

public String getData(){
    db.open();
    Cursor c = db.   ;
    return none;
}

db是我的数据库名称。我想知道是否有人知道为什么它不给我选择。当我输入查询时,它表示在DBAdapter中不存在hat。我是否必须在DBAdapter类中添加一些内容?如果是这样,我还需要添加什么?

这是我的活动Java:

package com.course.ucas;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.database.sqlite.SQLiteDatabase; 

public class DatabaseActivity extends Activity {
/** Called when the activity is first created. */
DBAdapter db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

DBAdapter db = new DBAdapter(this);
db.open();
long id;
id = db.insertTitle(
"UNIVERSITY OF ABERDEEN","Computer Science",310,84,0,"BBB","");
id = db.insertTitle(
"UNIVERSITY OF ABERTAY DUNDEE","Computer Science",300,0,70,"CCC","N/A");
id = db.insertTitle(
"ABERYSTWYTH UNIVERSITY","Computer Science",270,79,70,"BBC","N/A");
id = db.insertTitle(
"ANGLIA RUSKIN UNIVERSITY","Computer Science",220,69,50,"CCC","N/A");
id = db.insertTitle(
"ASTON UNIVERSITY","Computer Science",330,91,80,"BBB","N/A");
id = db.insertTitle(
"BANGOR UNIVERSITY","Computer Science",260,86,0,"CCC","N/A");
id = db.insertTitle(
"UNIVERSITY OF BATH","Computer Science",440,91,100,"AAA","A*AB");
id = db.insertTitle(
"UNIVERSITY OF BEDFORDSHIRE","Computer Science",120,74,50,"CCD","N/A");
id = db.insertTitle(
"UNIVERSITY OF BIRMINGHAM","Computer Science",400,76,90,"AAB","N/A");
id = db.insertTitle(
"BIRMINGHAM CITY UNIVERSITY","Computer Science",240,68,70,"BCC","N/A");
id = db.insertTitle(
"THE UNIVERSITY OF BOLTON","Computer Science",0,78,0,"CCC","N/A");
id = db.insertTitle(
"UNIVERSITY OF BRADFORD","Computer Science",180,72,50,"CCC","N/A");
id = db.insertTitle(
"UNIVERSITY OF BRIGHTON","Computer Science",270,80,69,"BBB","N/A");
id = db.insertTitle(
"UNIVERSITY OF BRISTOL","Computer Science",480,96,95,"AAA","N/A");
id = db.insertTitle(
"THE UNIVERSITY OF BUCKINGHAM","Computer Science",0,0,0,"BBC","N/A");
id = db.insertTitle(
"BUCKINGHAMSHIRE NEW UNIVERSITY","Computer Science",150,61,55,"CCD","N/A");
id = db.insertTitle(
"UNIVERSITY OF CAMBRIDGE","Computer Science",580,95,100,"A*AA","N/A");
id = db.insertTitle(
"CANTERBURY CHRIST CHURCH UNIVERSITY","Computer Science",170,81,45,"CCC","N/A");
id = db.insertTitle(
"CARDIFF UNIVERSITY","Computer Science",360,79,82,"ABB","N/A");
id = db.insertTitle(
"CARDIFF METROPOLITAN UNIVERSITY","Computer Science",200,93,55,"BBC","N/A");
id = db.insertTitle(
"UNIVERSITY OF CENTRAL LANCASHIRE","Computer Science",240,75,74,"CCC","BBC");
id = db.insertTitle(
"UNIVERSITY OF CHESTER","Computer Science",240,86,75,"CCC","N/A");
id = db.insertTitle(
"THE CITY UNIVERSITY","Computer Science",320,65,73,"AAA","N/A");
id = db.insertTitle(
"COVENTRY UNIVERSITY","Computer Science",230,67,65,"BBC","N/A");
id = db.insertTitle(
"THE UNIVERSITY OF CUMBRIA","Computer Science",0,0,0,"CCC","N/A");
id = db.insertTitle(
"DE MONTFORT UNIVERSITY","Computer Science",230,80,58,"AAA","N/A");
id = db.insertTitle(
"UNIVERSITY OF DERBY","Computer Science",260,73,70,"BBC","N/A");
id = db.insertTitle(
"UNIVERSITY OF DUNDEE","Computer Science",336,96,85,"BBB","N/A");
id = db.insertTitle(
"DURHAM UNIVERSITY","Computer Science",440,81,100,"AAA","N/A");
id = db.insertTitle(
"UNIVERSITY OF EAST ANGLIA","Computer Science",320,89,55,"BBB","N/A");
id = db.insertTitle(
"UNIVERSITY OF EAST LONDON","Computer Science",205,74,39,"CDD","N/A");
id = db.insertTitle(
"UNIVERSITY OF EDINBURGH","Computer Science",428,90,85,"AAA","ABB");
id = db.insertTitle(
"EDINBURGH NAPIER UNIVERSITY","Computer Science",260,83,58,"CCD","N/A");
id = db.insertTitle(
"UNIVERSITY OF ESSEX","Computer Science",315,84,75,"BBB","N/A");
id = db.insertTitle(
"UNIVERSITY OF EXETER","Computer Science",0,86,95,"ABB","N/A");
id = db.insertTitle(
"UNIVERSITY OF GLAMORGAN / PRIFYSGOL MORGANNWG","Computer Science",280,69,52,"BBB","N/A");
id = db.insertTitle(
"UNIVERSITY OF GLASGOW","Computer Science",384,98,90,"ABB","N/A");
id = db.insertTitle(
"GLASGOW CALEDONIAN UNIVERSITY","Computer Science",258,80,51,"CC","N/A");
id = db.insertTitle(
"UNIVERSITY OF GLOUCESTERSHIRE","Computer Science",220,73,71,"CCC","N/A");
id = db.insertTitle(
"GLYNDWR UNIVERSITY","Computer Science",200,75,70,"CCC","N/A");
id = db.insertTitle(
"GOLDSMITHS' COLLEGE","Computer Science",220,83,70,"BBB","N/A");
id = db.insertTitle(
"UNIVERSITY OF GREENWICH","Computer Science",180,89,54,"BBC","N/A");
id = db.insertTitle(
"HERIOT-WATT UNIVERSITY","Computer Science",288,77,90,"BBB","N/A");
id = db.insertTitle(
"UNIVERSITY OF HERTFORDSHIRE","Computer Science",180,80,62,"CDD","N/A");
id = db.insertTitle(
"UNIVERSITY OF THE HIGHLANDS AND ISLANDS","Computer Science",0,0,0,"D","N/A");
id = db.insertTitle(
"UNIVERSITY OF HUDDERSFIELD","Computer Science",240,63,84,"BBB","N/A");
id = db.insertTitle(
"UNIVERSITY OF HULL","Computer Science",240,83,95,"BBC","N/A");
id = db.insertTitle(
"IMPERIAL COLLEGE, LONDON","Computer Science",510,84,98,"AAA","N/A");
id = db.insertTitle(
"KEELE UNIVERSITY","Computer Science",260,74,0,"BBB","N/A");
id = db.insertTitle(
"UNIVERSITY OF KENT","Computer Science",310,80,90,"BBB","N/A");
id = db.insertTitle(
"KING'S COLLEGE LONDON","Computer Science",370,82,90,"AAB","N/A");
id = db.insertTitle(
"KINGSTON UNIVERSITY","Computer Science",200,73,68,"BBC","N/A");
id = db.insertTitle(
"LANCASTER UNIVERSITY","Computer Science",380,81,80,"ABB","N/A");
id = db.insertTitle(
"UNIVERSITY OF LEEDS","Computer Science",370,69,90,"AAA","N/A");
id = db.insertTitle(
"LEEDS METROPOLITAN UNIVERSITY","Computer Science",180,71,57,"BBC","N/A");
id = db.insertTitle(
"UNIVERSITY OF LEICESTER","Computer Science",330,92,80,"ABB","N/A");
id = db.insertTitle(
"UNIVERSITY OF LIVERPOOL","Computer Science",360,89,80,"AAB","ABB");
id = db.insertTitle(
"LIVERPOOL HOPE UNIVERSITY","Computer Science",280,78,40,"ABB","N/A");
id = db.insertTitle(
"UNIVERSITY COLLEGE LONDON","Computer Science",430,87,90,"A*AA","N/A");
id = db.insertTitle(
"LONDON METROPOLITAN UNIVERSITY","Computer Science",140,73,55,"ABB","N/A");
id = db.insertTitle(
"LOUGHBOROUGH UNIVERSITY","Computer Science",340,88,88,"ABB","N/A");
id = db.insertTitle(
"THE UNIVERSITY OF MANCHESTER","Computer Science",400,82,83,"AAB","N/A");
id = db.insertTitle(
"THE MANCHESTER METROPOLITAN UNIVERSITY","Computer Science",220,67,66,"CCC","N/A");
id = db.insertTitle(
"MIDDLESEX UNIVERSITY","Computer Science",140,73,50,"CDD","N/A");
id = db.insertTitle(
"NEWCASTLE UNIVERSITY","Computer Science",350,88,90,"ABB","N/A");
id = db.insertTitle(
"THE UNIVERSITY OF NORTHAMPTON","Computer Science",200,75,60,"BBB","N/A");
id = db.insertTitle(
"UNIVERSITY OF NORTHUMBRIA","Computer Science",240,80,77,"BBC","N/A");
id = db.insertTitle(
"UNIVERSITY OF NOTTINGHAM","Computer Science",360,82,90,"BBB","N/A");
id = db.insertTitle(
"NOTTINGHAM TRENT UNIVERSITY","Computer Science",260,69,68,"BBC","N/A");
id = db.insertTitle(
"OXFORD UNIVERSITY","Computer Science",520,0,90,"AAA","N/A");
id = db.insertTitle(
"OXFORD BROOKES UNIVERSITY","Computer Science",260,85,85,"BBC","N/A");
id = db.insertTitle(
"UNIVERSITY OF PLYMOUTH","Computer Science",260,76,75,"BBB","N/A");
id = db.insertTitle(
"UNIVERSITY OF PORTSMOUTH","Computer Science",240,81,72,"BBC","N/A");
id = db.insertTitle(
"QUEEN'S UNIVERSITY BELFAST","Computer Science",320,84,84,"BBB","N/A");
id = db.insertTitle(
"QUEEN MARY, UNIVERSITY OF LONDON","Computer Science",300,88,65,"BBB","N/A");
id = db.insertTitle(
"UNIVERSITY OF READING","Computer Science",350,79,95,"ABB","N/A");
id = db.insertTitle(
"THE ROBERT GORDON UNIVERSITY","Computer Science",240,79,80,"CCC","N/A");
id = db.insertTitle(
"ROYAL HOLLOWAY, UNIVERSITY OF LONDON","Computer Science",320,82,0,"BBB","N/A");
id = db.insertTitle(
"UNIVERSITY OF SALFORD","Computer Science",240,89,75,"BBB","N/A");
id = db.insertTitle(
"UNIVERSITY OF SHEFFIELD","Computer Science",390,83,95,"ABB","N/A");
id = db.insertTitle(
"SHEFFIELD HALLAM UNIVERSITY","Computer Science",240,73,73,"BBC","N/A");
id = db.insertTitle(
"UNIVERSITY OF SOUTHAMPTON","Computer Science",450,92,95,"AAA","N/A");
id = db.insertTitle(
"SOUTHAMPTON SOLENT UNIVERSITY","Computer Science",240,68,85,"CDD","N/A");
id = db.insertTitle(
"STAFFORDSHIRE UNIVERSITY","Computer Science",240,70,78,"CDD","N/A");
id = db.insertTitle(
"UNIVERSITY OF ST ANDREWS","Computer Science",458,90,0,"AAB","N/A");
id = db.insertTitle(
"UNIVERSITY OF STIRLING","Computer Science",255,100,65,"BBC","N/A");
id = db.insertTitle(
"UNIVERSITY OF STRATHCLYDE","Computer Science",333,97,90,"BBC","N/A");
id = db.insertTitle(
"UNIVERSITY OF SUNDERLAND","Computer Science",225,59,70,"BBB","N/A");
id = db.insertTitle(
"UNIVERSITY OF SURREY","Computer Science",380,89,90,"AAB","N/A");
id = db.insertTitle(
"UNIVERSITY OF SUSSEX","Computer Science",360,84,85,"ABB","BBB");
id = db.insertTitle(
"SWANSEA UNIVERSITY","Computer Science",280,82,75,"ABB","N/A");
id = db.insertTitle(
"TEESSIDE UNIVERSITY","Computer Science",260,78,64,"BBC","N/A");
id = db.insertTitle(
"UNIVERSITY OF ULSTER","Computer Science",237,73,63,"BBB","N/A");
id = db.insertTitle(
"UNIVERSITY OF WALES: TRINITY SAINT DAVID","Computer Science",0,77,0,"CDD","N/A");
id = db.insertTitle(
"UNIVERSITY OF WALES, NEWPORT","Computer Science",230,81,50,"CDD","N/A");
id = db.insertTitle(
"UNIVERSITY OF WARWICK","Computer Science",480,87,100,"AAB","N/A");
id = db.insertTitle(
"UNIVERSITY OF THE WEST OF ENGLAND, BRISTOL","Computer Science",240,74,84,"BBB","N/A");
id = db.insertTitle(
"THE UNIVERSITY OF WEST LONDON","Computer Science",0,37,0,"CDD","N/A");
id = db.insertTitle(
"THE UNIVERSITY OF WESTMINSTER","Computer Science",180,54,63,"CCC","AA");
id = db.insertTitle(
"UNIVERSITY OF THE WEST OF SCOTLAND","Computer Science",238,69,52,"BBC","N/A");
id = db.insertTitle(
"UNIVERSITY OF WOLVERHAMPTON","Computer Science",150,70,61,"CCD","N/A");
id = db.insertTitle(
"UNIVERSITY OF WORCESTER","Computer Science",210,65,55,"CCC","N/A");
id = db.insertTitle(
"UNIVERSITY OF YORK","Computer Science",460,91,100,"AAA","N/A");


  db.close();
}
public String getData(){
    db.open();
    Cursor c = db.rawQuery("SELECT * FROM titles", null);
    c.moveToFirst();   
    db.close();
    return c.getString(c.getColumnIndex("UCAS"));
  }
}

这是我的适配器:

package com.course.ucas;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

 public class DBAdapter {
public static final String KEY_ROWID = "_id";
public static final String KEY_UNIVERSITY = "University";
public static final String KEY_COURSE = "Course";   
public static final String KEY_UCAS = "UCAS";
public static final String KEY_SATISFACTION = "Satisfaction";
public static final String KEY_EMPLOYED = "Employed";
public static final String KEY_OFFER = "Offer";
public static final String KEY_OTHER = "Other";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "UCAS";
private static final String DATABASE_TABLE ="titles";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table titles (_id integer primary key autoincrement, University text not null, Course text not null, UCAS int not null, Satisfaction int not null, Employed int not null, Offer text not null, Other text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@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 titles");
onCreate(db);
}
}
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close()
{
DBHelper.close();
}
//---insert a title into the database---
public long insertTitle(String university, String other, int i, int j,int k, String   UCAS, String employed)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_UNIVERSITY, university);
initialValues.put(KEY_COURSE, j);
initialValues.put(KEY_UCAS , UCAS);
initialValues.put(KEY_SATISFACTION, k);
initialValues.put(KEY_EMPLOYED , employed);
initialValues.put(KEY_OFFER , i);
initialValues.put(KEY_OTHER , other);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---deletes a particular title---
public boolean deleteTitle(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID +
"=" + rowId, null) > 0;
}
//---retrieves all the titles---
public Cursor getAllTitles()
{
return db.query(DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_UNIVERSITY,
KEY_COURSE,
KEY_UCAS ,
KEY_SATISFACTION,
KEY_EMPLOYED,
KEY_OFFER,
KEY_OTHER,
},
null,
null,
null,
null,
null);
}
//---retrieves a particular title---
public Cursor getTitle(long rowId) throws SQLException
{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {
KEY_ROWID,
KEY_UNIVERSITY,
KEY_COURSE,
KEY_UCAS,
KEY_SATISFACTION,
KEY_EMPLOYED,
KEY_OFFER,
KEY_OTHER,
},
KEY_ROWID + "=" + rowId,
null,
null,
null,
null,
null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
//---updates a title---
public boolean updateTitle(long rowID, String university, String other, int i, int j,int k, String UCAS, String employed)
{
ContentValues args = new ContentValues();
args.put(KEY_UNIVERSITY, university);
args.put(KEY_COURSE, i);
args.put(KEY_UCAS , UCAS);
args.put(KEY_SATISFACTION, j);
args.put(KEY_EMPLOYED , employed);
args.put(KEY_OFFER , k);
args.put(KEY_OTHER , other);
return db.update(DATABASE_TABLE, args,
KEY_ROWID + "=" + rowID, null) > 0;
}

public String[] columns = new String[]{ KEY_ROWID, KEY_UNIVERSITY, KEY_COURSE,  KEY_UCAS, KEY_SATISFACTION, KEY_EMPLOYED, KEY_OFFER, KEY_OTHER };
}

3 个答案:

答案 0 :(得分:5)

所以你可以使用两种方法:

区别在于,query()默认情况下针对的sql injections针对rawQuery()方法提供了保护。

但您很可能需要阅读一些教程,因此请检查Android SQLite Database Tutorial

答案 1 :(得分:4)

这是您提取数据的方式。'查询'不是您应该在此处使用的关键字,而是使用' rawQuery'

   public String getData(){
        String s;
        db.open();
        Cursor c = db.rawQuery("SELECT * FROM Table", null);
        c.moveToFirst();   
        s=c.getString(c.getColumnIndex("columnName"));
        db.close();
        return s
      }

答案 2 :(得分:0)

你有一个适配器。为什么不用你。像这样。

public String getData(){
  db.open();
  Cursor c = db.getAllTitles();
  c.moveToFirst();   
  db.close();
  return c.getString(c.getColumnIndex("UCAS"));
}