gpt4 book ai didi

android - sqlite 游标返回列的错误值

转载 作者:行者123 更新时间:2023-11-29 20:16:28 25 4
gpt4 key购买 nike

为什么我从 url 列的数据库 null 中获取?

我像这样在sqlite中添加数据

  void addObjects(Objects object) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();

values.put(OBJECT_ID,"10" ); // OBJECT Name
values.put(OBJECT_NAME, "H"); // OBJECT Name
values.put(OBJECT_URL, "http://api.androidhive.info/images/sample.jpg"); // OBJECT URL
values.put(OBJECT_TYPE, "image"); // Contact type
System.out.println(values);

// Inserting Row
db.insert(TABLE_OBJECTS, null, values);
db.close(); // Closing database connection
}

日志值的结果www.i I/System.out:TYPE=image NAME=H ID=10 URL=http://api.androidhive.info/images/sample.jpg

然而,当我得到所有结果时,我有 url = null

public List<Objects> getAllObjects() {
List<Objects> Objectslist = new ArrayList<Objects>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_OBJECTS;

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Objects object = new Objects();
Log.d("OBJECT_URL", OBJECT_URL);

object.setId(cursor.getInt(cursor.getColumnIndex(OBJECT_ID)));
object.setName(cursor.getString(cursor.getColumnIndex(OBJECT_NAME)));
object.setUrl(cursor.getString(cursor.getColumnIndex(OBJECT_URL)));
// Adding contact to list
Objectslist.add(object);
} while (cursor.moveToNext());
}

这是我正在获取 sqlite 结果的主要 Activity

List<Objects> Objects = db.getAllObjects();
Log.d("he","tttttt");
System.out.println(Objects);
DBadapter adapter = new DBadapter(getApplicationContext(), R.layout.grid_item_layout, Objects);

这是系统输出的结果

[Objects{id=10, name='http://api.androidhive.info/images/sample.jpg', url='null', type='null', category='null'}, Objects{id=10, name='http://api.androidhive.info/images/sample.jpg', url='null', type='null', category='null'}

这是完整的 sqlite 类

     public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;



// Contacts table name
private static final String TABLE_OBJECTS = "OBJECTS";
private static final String OBJECT_ID="ID";
private static final String OBJECT_NAME="NAME";
private static final String OBJECT_TYPE ="TYPE";
private static final String OBJECT_URL ="URL";
private static final String OBJECT_CATEGORY ="CATEGORY";
private static final String OBJECT_DATE_ADDED ="DATE_ADDED";
private static final String OBJECT_USERS_ID ="USERS_ID";
private static final String OBJECT_HIDE_YN ="HIDE_YN";


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

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL("DROP TABLE IF EXISTS " + TABLE_OBJECTS);

String CREATE_OBJECTS_TABLE ="CREATE TABLE " +TABLE_OBJECTS+"("
+ OBJECT_ID +" INTEGER PRIMARY KEY AUTOINCREMENT,"
+ OBJECT_NAME +" VARCHAR(100) NULL,"
+ OBJECT_TYPE +" VARCHAR(10) NULL,"
+ OBJECT_URL +" VARCHAR(1000) NULL,"
+OBJECT_CATEGORY +" VARCHAR(50) NULL,"
+OBJECT_DATE_ADDED +" DATETIME NULL,"
+OBJECT_USERS_ID +" NUMERIC(10) NULL,"
+OBJECT_HIDE_YN +" VARCHAR(1) NULL) ";
db.execSQL(CREATE_OBJECTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_OBJECTS);

// Create tables again
onCreate(db);
}

/**
* All CRUD(Create, Read, Update, Delete) Operations
*/

// Adding new contact
void addObjects(Objects object) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();

values.put(OBJECT_ID,"10" ); // OBJECT Name
values.put(OBJECT_NAME, "H"); // OBJECT Name
values.put(OBJECT_URL, "http://api.androidhive.info/images/sample.jpg"); // OBJECT URL
values.put(OBJECT_TYPE, "image"); // Contact type
System.out.println(values);

// Inserting Row
long id = db.insert(TABLE_OBJECTS, null, values);
System.out.println( "id:"+id);
db.insert(TABLE_OBJECTS, null, values);
db.close(); // Closing database connection
}
// Objects object=new Objects(String );

// Getting single contact
Objects geturl(String name) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_OBJECTS, new String[] {OBJECT_URL,
OBJECT_NAME , OBJECT_CATEGORY, OBJECT_TYPE }, OBJECT_NAME + "=?",
new String[] { String.valueOf(name) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();

Objects object = new Objects(cursor.getString(0),cursor.getString(1),
cursor.getString(2),cursor.getString(3));

return object;
}

// Getting All Contacts
public List<Objects> getAllObjects() {
List<Objects> Objectslist = new ArrayList<Objects>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_OBJECTS;

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Objects object = new Objects();
Log.d("OBJECT_URL", OBJECT_URL);

object.setId(cursor.getInt(cursor.getColumnIndex(OBJECT_ID)));
object.setName(cursor.getString(cursor.getColumnIndex(OBJECT_NAME)));
object.setUrl(cursor.getString(cursor.getColumnIndex(OBJECT_URL)));
// Adding contact to list
Objectslist.add(object);
} while (cursor.moveToNext());
}

// return contact list
return Objectslist;
}

// Updating single contact
public int updateContact(Objects object) {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put(OBJECT_NAME, object.getName()); // OBJECT Name
values.put(OBJECT_URL, object.getUrl()); // OBJECT URL
values.put(OBJECT_TYPE, object.getType()); // Contact type
values.put(OBJECT_CATEGORY, object.getCategory()); // Contact category

// updating row
return db.update(TABLE_OBJECTS, values, OBJECT_ID + " = ?",
new String[] { String.valueOf(object.getId()) });
}

// Deleting single contact
public void deleteContact(Objects objects) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_OBJECTS, OBJECT_ID + " = ?",
new String[] { String.valueOf(objects.getId()) });
db.close();
}


// Getting contacts Count
public int getObjectsCount() {
String countQuery = "SELECT * FROM " + TABLE_OBJECTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();

// return count
return cursor.getCount();
}

}

最佳答案

在您的 Objects geturl(String name) 方法中,更改

Cursor cursor = db.query(TABLE_OBJECTS, new String[] {OBJECT_URL,
OBJECT_NAME , OBJECT_CATEGORY, OBJECT_TYPE }, OBJECT_NAME + "=?",
new String[] { String.valueOf(name) }, null, null, null, null);

Cursor cursor = db.query(TABLE_OBJECTS, new String[] {OBJECT_ID,
OBJECT_NAME , OBJECT_URL, OBJECT_TYPE }, OBJECT_NAME + "=?",
new String[] { String.valueOf(name) }, null, null, null, null);

假设您的 Objects 类具有以下结构:

public class Objects {
private String id;
private String name;
private String url;
private String type;

// methods
}

您传递给 db.query() 方法必须遵循与 Objects 类中相同的顺序(首先是 id,第二个 name,第三个 url,第四个 输入)

关于android - sqlite 游标返回列的错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33789991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com