Android SQLiteException。为什么会这样?

时间:2017-08-19 15:10:48

标签: java android android-sqlite

这是从class User < ActiveRecord::Base # as parent has_many: student_relations, foreign_key: :parent_id, class_name: "ParentStudent" has_many: students, through: :student_relations, source: :student # as student has_many: parent_relations, foreign_key: :student_id, class_name: "ParentStudent" has_many: parents, through: :parent_relations, source: :parent end class ParentStudent < ActiveRecord::Base belongs_to :student, foreign_key: "student_id", class_name: "User" belongs_to :parent, foreign_key: "parent_id", class_name: "User" end 获取表格 RECORD 的代码。在SQLiteDatebase出现问题时,它总是抛出try block。我在这里尝试的只是将表“RECORD”的每一行转换为字符串并将其添加到SQLiteException,然后将数组连接到ArrayList,最后使用{{1显示数组。

ArrayAdapter

这是我的datebaseHelper的代码:

ListView

package com.example.tommy.stop_watch_real; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteOpenHelper; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.CursorAdapter; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.Toast; import java.util.ArrayList; public class RecordList extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_record_list); ListView temp = (ListView) findViewById(R.id.recordList); ArrayList<String> arrayList = new ArrayList<String>(); ArrayAdapter<String> adapter; **try { SQLiteOpenHelper dataBaseHelper = new DataBaseHelper(this); SQLiteDatabase db = dataBaseHelper.getReadableDatabase(); Cursor cursor = db.query("RECORD", new String[] {"_id, DATE, MINUTES"}, null, null,null,null,null); while (cursor.moveToFirst()){ String id = Integer.toString(cursor.getInt(0)); String date = cursor.getString(1); String minutes = Integer.toString(cursor.getInt(2)); String finalValue = id + "|" + date + "|" + minutes; arrayList.add(finalValue); } adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList); temp.setAdapter(adapter); cursor.close(); db.close();}** catch (SQLiteException e) { Toast.makeText(RecordList.this, "Database not available", Toast.LENGTH_LONG).show(); } } } 始终显示,意味着class DataBaseHelper extends SQLiteOpenHelper { private static final String DB_NAME = "Record"; // name of the database private static final int DB_VERSION = 1; //Version DataBaseHelper (Context context){ super(context, DB_NAME, null, DB_VERSION); } public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE RECORD (" + "_id INTEGER PRIMARY KEY AUTOINCERMENT, " + "DATE STRING," //careful + "MINUTES INTEGER" + ");" ); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { //no need to worry about this yet. } } 出现问题。我刚刚开始一周前的android开发,我不知道我的代码出了什么问题。如果你能给我一些指导,那就太好了。

提前谢谢你。

1 个答案:

答案 0 :(得分:1)

这部分对我来说似乎不对。

 while (cursor.moveToFirst()){
            String id = Integer.toString(cursor.getInt(0));
            String date = cursor.getString(1);
            String minutes = Integer.toString(cursor.getInt(2));
            String finalValue = id + "|" + date + "|" + minutes;
            arrayList.add(finalValue);
        }

我不确定您要实现的目标,但从数据库获取数据的正确方法如下:

 cursor.moveToFirst()
 while (cursor.isAfterLast()){
            String id = Integer.toString(cursor.getInt(0));
            String date = cursor.getString(1);
            String minutes = Integer.toString(cursor.getInt(2));
            String finalValue = id + "|" + date + "|" + minutes;
            arrayList.add(finalValue);
        }