随机使用查询

时间:2017-01-31 20:45:18

标签: android android-sqlite

您好我正在开发一个类似于词汇卡的基本项目。我在数据库中添加了一些英语单词和土耳其语含义,我只得到了#34; word"来自db的数据。我想得到它的id和土耳其语的含义。我怎么能这样做?也, 我不知道查询括号中的其他空值。如何获取相关信息。

这是db code:

public class DataBase extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "DBWORDS";
private static final String TABLE_NAME  = "WTABLE";
private static final String ID  ="ID";
private static final String WORD   = "WORD";
private static final String WORD_TR1 = "TURKCE_1";


public DataBase(Context context){
    super(context,DATABASE_NAME,null,DATABASE_VERSION);
}

public DataBase(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_DBTABLE = "CREATE TABLE " + TABLE_NAME + "(" + ID +" INTEGER PRIMARY KEY AUTOINCREMENT," +WORD+
            " TEXT," +WORD_TR1+" TEXT" + "  )";
    db.execSQL(CREATE_DBTABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String TABLE_DELETE = "DROP TABLE IF EXISTS" + TABLE_NAME ;
    db.execSQL(TABLE_DELETE);
    onCreate(db);

}

public long addRecord(Words words){
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(WORD,words.getWORD());
    contentValues.put(WORD_TR1,words.getTR1());
    long status = sqLiteDatabase.insert(TABLE_NAME,null,contentValues);
    sqLiteDatabase.close();
    return status;
}
public List<Words> allRecords(){
    List<Words> wordsList = new ArrayList<>();
    String sql_query = "SELECT *FROM " + TABLE_NAME;
    SQLiteDatabase sqLiteDatabase =this.getWritableDatabase();
    Cursor cursor =sqLiteDatabase.rawQuery(sql_query,null);
    if(cursor.moveToFirst()){
        Words words =null;
        do{
            words = new Words();
            words.setID(cursor.getInt(0));
            words.setWORD(cursor.getString(1));
            words.setTR1(cursor.getString(2));
            wordsList.add(words);
        }while (cursor.moveToNext());
    }
    return wordsList;
}

public String getRandomQuote(){
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    Cursor cursor = sqLiteDatabase.query(TABLE_NAME + " ORDER BY RANDOM() LIMIT 1",new String[]
            {WORD},null,null,null,null,null);
    if(cursor.moveToFirst()){
        return cursor.getString(cursor.getColumnIndex(WORD));
    }
    else
        return "nothing";
}

主要活动:

 btnStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(),WordsActivity.class);
            DataBase dataBase = new DataBase(getApplicationContext());
           String random =  dataBase.getRandomQuote();
            intent.putExtra("random",random);
            startActivity(intent);
        }
    });

单词活动:

        textView = (TextView)findViewById(R.id.textView);
        Intent intent= getIntent();
        String random =intent.getStringExtra("random");
         textView.setText("Number : "+ random);

之后我得到了#34;你好&#34;在文本中。 我想&#34;你好&#34; &#34; Merhaba&#34; &#34; 21&#34;

1 个答案:

答案 0 :(得分:0)

您只从查询中的表中获取WORD。您需要 WORD_TR1 (假设是翻译)和 ID 。所以你需要告诉查询返回那些列。然后将它们连接到返回的字符串。所有这些都在 getRandomQuote 方法中。

e.g。

public String getRandomQuote(){
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    Cursor cursor = sqLiteDatabase.query(TABLE_NAME + " ORDER BY RANDOM() LIMIT 1",new String[]
            {WORD, WORD_TR1, ID},null,null,null,null,null);
    if(cursor.moveToFirst()){
        return cursor.getString(cursor.getColumnIndex(WORD)) +
        cursor.getString(cursor.getColumnIndex(WORD_TR1)) +
        cursor.getString(cursor.getColumnIndex(ID));
    }
    else
        return "nothing";
}

我认为这就是你想要的(注意没有测试,所以可能是一些错误)。