1

我有一个有线问题。我写了一个使用 sqlite 的代码。这是相关的行:

  private final String DB_NAME = "cus";
    private final String TABLE_NAME = "cus2";
    SQLiteDatabase customersDB2 = null;
    /** Called when the activity is first created. */
    private void mFillDbsTable(){
        try {
            customersDB2.execSQL("INSERT INTO " + TABLE_NAME + " Values ( 1, 'the ', 'ה- ');");
            customersDB2.execSQL("INSERT INTO " + TABLE_NAME + " Values ( 2, 'of ', 'של ');");
            customersDB2.execSQL("INSERT INTO " + TABLE_NAME + " Values ( 3, 'to ', 'אל ');");
            customersDB2.execSQL("INSERT INTO " + TABLE_NAME + " Values ( 4, 'and ', 'ו - ');");
            customersDB2.execSQL("INSERT INTO " + TABLE_NAME + " Values ( 5, 'a ', 'מופיע לפני שם עצם ביחיד ללא הא הידיעה ');");
            customersDB2.execSQL("INSERT INTO " + TABLE_NAME + " Values ( 6, 'in ', 'בתוך ');");
            customersDB2.execSQL("INSERT INTO " + TABLE_NAME + " Values ( 7, 'for ', 'עבור ');");
    ................
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_learn);
        try {
            customersDB2 =  this.openOrCreateDatabase(  DB_NAME, MODE_PRIVATE, null);
            customersDB2.execSQL("CREATE TABLE " +
                    TABLE_NAME +
                    " (Block INT," +
                    " Name VARCHAR, Street VARCHAR);");
            mFillDbsTable();
}catch (SQLiteException se ) {
            Log.e(getClass().getSimpleName(), "create/Open the database problem");
}

现在,当我询问查询时:

    correctAnswer=oneTOfour.nextInt(4);
    Toast.makeText(this,"correctAnswerק"+correctAnswer,Toast.LENGTH_SHORT).show();
    correctAnswerTHE_ANSWER =(r.nextInt(1000)) + 1;
    Toast.makeText(this,"correctAnswerTHE_ANSWER"+correctAnswerTHE_ANSWER,Toast.LENGTH_SHORT).show();

      ArrayList<String> results = new ArrayList<String>();
    Cursor c = customersDB2.rawQuery("SELECT Name, Street  FROM "    +
            TABLE_NAME +
            " where Block = 66 ", null);


    if (c != null ) { //true

         Toast.makeText(this,"c.getColumnIndex(Name)" + c.getColumnIndex("Name"),Toast.LENGTH_SHORT).show(); //got 0!!
         if  (c.moveToFirst()) {   
            do {

                 name = c.getString(c.getColumnIndex("Name"));
                 street = c.getString(c.getColumnIndex("Street"));          
                 results.add(name + ", " + street );
            }while (c.moveToNext());
        }
    }

    else{
         Toast.makeText(this,"null2",Toast.LENGTH_SHORT).show();
    }


    question.setText(name);  //this is null

}

它没有达到第二个如果

也许我第一次运行这个应用程序时只有 2 列。我试图打开其他项目,更改表的名称..没有任何工作。一些想法?

4

2 回答 2

1
"SELECT Name, Street  FROM " ...

当您命名要选择的字段时,它会按该顺序返回它们,Name 第一列或0.

如果您希望它们按照创建它们的顺序排列,请尝试SELECT * FROM

问题是,它们的顺序有什么关系?你已经在使用c.getColumnIndex()了,所以他们的顺序没有意义。

于 2012-10-18T18:11:28.920 回答
0

可能与您插入的 UTF 非标准字符有关。您应该尝试使用 ContentValues 插入,例如:

ContentValues row = new ContentValues();
row.put("Name", "blah");
row.put("Street", "your utf strings");
row.put("Block", 1);
long id = db.insert(TABLE_NAME, null, row);
于 2012-10-18T18:13:30.107 回答