2014-05-15 3 views
-2

나는 비슷한 질문에 대한 많은 답변을 이미 읽었지만 그 대답은 내 문제를 해결하지 못하는 것 같습니다. 안드로이드 - 가져온 데이터베이스에서 무작위 데이터를 읽는 방법

나는 내 응용 프로그램의 "자산"폴더에 SQLite는 데이터베이스를 복사, 나는 (내가 BTW이 코드에서 모든 것을 이해하지 않았다) 웹 사이트에서 발견이 코드로 수입 :

package com.example.quizone_2; 

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import android.content.Context; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteException; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DataBase extends SQLiteOpenHelper{ 

    //The Android's default system path of your application database. 
    private static String DB_PATH = "/data/data/com.example.quizone_2/databases/"; 

    private static String DB_NAME = "QuizOne_DB"; 

    private SQLiteDatabase myDataBase; 

    private final Context myContext; 

    /** 
    * Constructor 
    * Takes and keeps a reference of the passed context in order to access to the  application assets and resources. 
    * @param context 
     */ 
    public DataBase(Context context) { 

    super(context, DB_NAME, null, 1); 
    this.myContext = context; 
} 

    /** 
* Creates a empty database on the system and rewrites it with your own database. 
* */ 
public void createDataBase() throws IOException{ 

    boolean dbExist = checkDataBase(); 

    if(dbExist){ 
     //Do nothing - database already exist 
    }else{ 

     //By calling this method and empty database will be created into the default system path 
      //of your application so we are gonna be able to overwrite that database with our database. 
     this.getReadableDatabase(); 

     try { 

      copyDataBase(); 

     } catch (IOException e) { 

      throw new Error("Error copying database"); 

     } 
    } 

} 

/** 
* Check if the database already exist to avoid re-copying the file each time you open the application. 
* @return true if it exists, false if it doesn't 
*/ 
private boolean checkDataBase(){ 

    SQLiteDatabase checkDB = null; 

    try{ 
     String myPath = DB_PATH + DB_NAME; 
     checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 

    }catch(SQLiteException e){ 

     //database does't exist yet. 

    } 

    if(checkDB != null){ 

     checkDB.close(); 

    } 

    return checkDB != null ? true : false; 
} 

/** 
* Copies your database from your local assets-folder to the just created empty database in the 
* system folder, from where it can be accessed and handled. 
* This is done by transfering bytestream. 
* */ 
private void copyDataBase() throws IOException{ 

    //Open your local db as the input stream 
    InputStream myInput = myContext.getAssets().open(DB_NAME); 

    // Path to the just created empty db 
    String outFileName = DB_PATH + DB_NAME; 

    //Open the empty db as the output stream 
    OutputStream myOutput = new FileOutputStream(outFileName); 

    //transfer bytes from the inputfile to the outputfile 
    byte[] buffer = new byte[1024]; 
    int length; 
    while ((length = myInput.read(buffer))>0){ 
     myOutput.write(buffer, 0, length); 
    } 

    //Close the streams 
    myOutput.flush(); 
    myOutput.close(); 
    myInput.close(); 

} 

public void openDataBase() throws SQLException{ 

    //Open the database 
    String myPath = DB_PATH + DB_NAME; 
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 

} 

@Override 
public synchronized void close() { 

     if(myDataBase != null) 
      myDataBase.close(); 

     super.close(); 

} 

@Override 
public void onCreate(SQLiteDatabase db) { 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

} 

    // Add your public helper methods to access and get content from the database. 
    // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy 
    // to you to create adapters for your views. 

} 

내 데이터베이스에는 질의 응답이 포함 된 테이블이 있습니다.

내가 원했던 것은 무작위 질문을 검색하여 활동에 표시하고 답변 (true 또는 false)을 데이터베이스의 답변과 비교하는 것입니다.

문제는 웹에서 데이터를 읽는 방법을 실제로 이해하지 못했기 때문에 데이터베이스가 가져와서 앱에서 생성되지 않았고 두 번째로 웹에서 비슷한 것을 찾지 못했습니다. 내 데이터베이스. 말할 것도없이 나는 무작위로 검색하는 법을 모른다.

질문을 전적으로 읽고 고맙게 여길 수 있습니다. 누군가를 도울 수 있기를 바랍니다. 어디서부터 시작해야할지 몰라서.

당신에게 대단히 감사합니다

답변

0

하나의 대안 : 1) 테이블의 각 질문에 숫자 ID를 할당합니다. (1에서 n까지 n은 질문의 수입니다.) 2) 활동에서 1과 n 사이의 난수를 생성하십시오. Math.round ((Math.random() * n) +1)를 사용하면됩니다. 3) 그런 다음 숫자 ID (1 점)가 임의의 숫자와 같은 테이블을 쿼리합니다.

+0

매우 흥미 롭습니다. 감사합니다. 어떻게해야합니까? 내 말은, 코드에 대해 말하면, 난수와 행 ID를 어떻게 비교할 수 있을까요? 그런 다음 어떻게 해당 행의 텍스트를 가져올 수 있습니까? 도움이 많이 필요하면 죄송합니다. – Solaire

관련 문제