Select语句不适用于sqlite android
我是android开发新手。我想要的是从数据库中存储的值填充我的列表。因此,我使用insert和select语句创建了一个数据库。我还创建了一个模型来保存数据。我从列表适配器将数据插入数据库,但当我试图检索该数据时,我得到了null 数据库类Select语句不适用于sqlite android,android,list,android-sqlite,Android,List,Android Sqlite,我是android开发新手。我想要的是从数据库中存储的值填充我的列表。因此,我使用insert和select语句创建了一个数据库。我还创建了一个模型来保存数据。我从列表适配器将数据插入数据库,但当我试图检索该数据时,我得到了null 数据库类 public class DbHandler extends SQLiteOpenHelper { private static final int dbVersion = 1; private static final String d
public class DbHandler extends SQLiteOpenHelper {
private static final int dbVersion = 1;
private static final String dbName = "HSsuraksha";
private static final String tableName = "pocketDocs";
private static final String userId = "userId";
private static final String docId = "docId";
private static final String fileName = "fileName";
private static final String fileExt = "fileExt";
private static final String title = "title";
private static final String createTable = "CREATE TABLE " + tableName + "(" + userId + " Integer Primary Key," + docId + " Integer," + fileName + " Text," + fileExt + " Text," + title + " Text" + ")";
public DbHandler(Context context) {
super(context, dbName, null, dbVersion);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
}
public void insertData(DbModel dbModel) {
SQLiteDatabase database = getWritableDatabase();
ContentValues contentValues = new ContentValues();
database.beginTransaction();
contentValues.put(userId, dbModel.userId);
contentValues.put(docId, dbModel.docId);
contentValues.put(fileName, dbModel.fileName);
contentValues.put(fileExt, dbModel.fileExtension);
contentValues.put(title, dbModel.title);
if (contentValues != null) {
Long id = database.insert(tableName, null, contentValues);
Log.e("insert values", "" + id);
}
database.endTransaction();
database.close();
}
public DbModel selectDocs(String id) {
SQLiteDatabase db = this.getReadableDatabase();
DbModel model = new DbModel();
Cursor cursor = db.query(tableName, new String[]{fileName, fileExt, title}, docId + "=?", new String[]{id}, null, null, null, null);
if (cursor.moveToFirst()) {
model.fileName = cursor.getString(2);
model.fileExtension = cursor.getString(3);
model.title = cursor.getString(4);
}
db.close();
return model;
}
}
数据库模型
public class DbModel implements Serializable {
public String userId, docId, fileName, fileExtension, title;
public DbModel() {
}
public DbModel(String userId, String docId, String fileName, String fileExtension, String title) {
this.userId = userId;
this.docId = docId;
this.fileName = fileName;
this.fileExtension = fileExtension;
this.title = title;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getDocId() {
return docId;
}
public void setDocId(String docId) {
this.docId = docId;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileExtension() {
return fileExtension;
}
public void setFileExtension(String fileExtension) {
this.fileExtension = fileExtension;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
列表适配器
public View getView(int i, View view, ViewGroup viewGroup) {
final ViewHolder holder;
dbHandler = new DbHandler(context);
dbModel = new DbModel("1", "1", "GoogleDocs", ".pdf", "Test");
dbHandler.insertData(dbModel);
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.view_docs_custom_list, viewGroup, false);
holder.docsImage = (ImageView) view.findViewById(R.id.iv_docs_image);
holder.tvTitle = (TextView) view.findViewById(R.id.tv_file_name);
holder.tvPostedOn = (TextView) view.findViewById(R.id.tv_posted_on);
holder.btShare = (Button) view.findViewById(R.id.bt_share);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
dbHandler.selectDocs(dbModel.getDocId()); // here i am getting the values from db and displaying it in the list
holder.tvTitle.setText(dbModel.title);
改变
到
getString
基于零索引。它取决于您在query
中提到的列数,而不是在CREATE
语句中使用它们的顺序
如果你想做
cursor.getString(3);
您应该得到一个非法状态异常
声明
java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
因为您试图访问
光标中的第4列,而它只有3列dbHandler.selectDocs(dbModel.getDocId())代码>返回模型对象。你是说这个dbModel.getDocId()
失败??在select方法“dbModel model=new dbModel();”中,model retuns null你确定吗?我不认为model
为null。您是否获得了NullPointerException
?如果是这样发布stacktraceno,我不会得到null指针异常,但当我调试它时,我会在调试应用程序时,在这行“If(cursor.moveToFirst())”之后,将DbModel中的所有值都作为null,它不会进入If backet,当数据库中没有数据时,directlt就会出现。您需要调用database.setTransactionSuccessful()
在数据库.endTransaction()之前代码>在您的insertData
方法中,感谢它的工作nw,但在“model.fileExtension=cursor.getString(3);”行中,我得到了错误
cursor.getString(3);
java.lang.IllegalStateException: Couldn't read row 0, col 3 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.