查询与光标在Android上工作的SQLite光标、工作、Android、SQLite

2023-09-13 02:27:10 作者:客串情人

不知道如果我是唯一一个谁觉得这...

我找到了SQLite的API在屁股和pretty的灵魂毁灭的工作在Android中一个完整的疼痛。有没有人有任何提示/佣工,使我的生活更容易?

下面是什么我谈论的例子。

  //创建code

db.execSQL(CREATE TABLE+ CUSTOMER_TABLE_NAME +(
                        + GENERIC_ID_KEY +INTEGER PRIMARY KEY NOT NULL,
                        + PHONE_KEY +INTEGER NOT NULL,
                        + CUSTOMER_NAME_KEY +TEXT NOT NULL,
                        + EMAIL_KEY +TEXT NOT NULL,
                        + ADDRESS_KEY +TEXT););


//获取code
    光标mCursor = mDb.query(真,CUSTOMER_TABLE_NAME,新的String [] {GENERIC_ID_KEY,
                        ADDRESS_KEY,PHONE_KEY,EMAIL_KEY,CUSTOMER_NAME_KEY},GENERIC_ID_KEY +=+ customerDbId,空,
                                NULL,NULL,NULL,NULL);

        客户客户=新客户(customerDbId,(CharSequence中)mCursor.getString(mCursor.getColumnIndexOrThrow(CUSTOMER_NAME_KEY)),
                            (CharSequence的)mCursor.getString(mCursor.getColumnIndexOrThrow(PHONE_KEY)),
                            (CharSequence的)mCursor.getString(mCursor.getColumnIndexOrThrow(EMAIL_KEY)),
                            (CharSequence中)mCursor.getString(mCursor.getColumnIndexOrThrow(ADDRESS_KEY)));
 

此创建从数据库查询一个简单的客户对象的简单exmple;我的一些code是远远比这厉害。手这样各具特色的查询,导致所有的排序错误,我没有发现,直到运行时做的。

任何提示大大AP preiciated!

的提示后,单击确定下面我现在有这样的:

  db.execSQL(CREATE TABLE客户(_id INTEGER PRIMARY KEY NOT NULL,
                        +PHONE_NUMBER INTEGER NOT NULL,
                        +Name文本NOT NULL,
                        +电子邮件文本NOT NULL,
                        +地址文本););


    //获取code
串Q =SELECT * FROM客户WHERE _id =+ customerDbId +;
        光标mCursor = mDb.rawQuery(Q,空);

        客户客户=新客户(mCursor);
 
Android Studio编辑光标变粗的解决方法

在客户,我进入字段像这样

  MNAME = cursor.getString(2)
 

啊,我感觉好多了:)

干杯 思

解决方案 请不要使用模型对象,如果你没有。我已经得出结论,除非是只能重新$ P $通过模型对象psented显著的业务逻辑,他们更麻烦比他们的价值在一个移动平台。 假设你被卡住的模型对象,让他们加载自己出的光标,而不是试图通过在许许多多的参数。 查询() rawQuery更详细的()对有限的附加值,如果你知道SQL。 组装你的 CREATE TABLE 通过串联子句是自我强加的痛苦,而不是SQLite的或Android授权。 请不要使用 getColumnIndexOrThrow()从自定义编写code。你写的查询,让你知道什么顺序列回来。只有在使用类似 getColumnIndexOrThrow()如果您要创建一些抽象的库,不知道的细节在游标给了它。 字符串的CharSequence 继承,因此,所有这些强制转换可以被丢弃。

Not sure if I'm the only one who feels this...

I find working with the sqlite api in android a complete pain in the butt and pretty soul destroying. Has anyone got any tips/helpers to make my life easier?

Here's an example of what I'm talking about.

//create code

db.execSQL("CREATE TABLE " + CUSTOMER_TABLE_NAME + " ("
                        + GENERIC_ID_KEY+ " INTEGER PRIMARY KEY NOT NULL, " 
                        + PHONE_KEY + " INTEGER NOT NULL, "
                        + CUSTOMER_NAME_KEY+ " TEXT NOT NULL, "
                        + EMAIL_KEY + " TEXT NOT NULL, "
                        + ADDRESS_KEY +" TEXT);");


//get code
    Cursor mCursor = mDb.query(true, CUSTOMER_TABLE_NAME, new String[] {GENERIC_ID_KEY,
                        ADDRESS_KEY, PHONE_KEY, EMAIL_KEY,CUSTOMER_NAME_KEY}, GENERIC_ID_KEY + "=" + customerDbId, null,
                                null, null, null, null);

        Customer customer = new Customer (customerDbId, (CharSequence)mCursor.getString(mCursor.getColumnIndexOrThrow(CUSTOMER_NAME_KEY)),
                            (CharSequence)mCursor.getString(mCursor.getColumnIndexOrThrow(PHONE_KEY)),
                            (CharSequence)mCursor.getString(mCursor.getColumnIndexOrThrow(EMAIL_KEY)),
                            (CharSequence)mCursor.getString(mCursor.getColumnIndexOrThrow(ADDRESS_KEY)));

This a simple exmple of creating a simple customer object from a db query; some of my code is far nastier than this. Hand crafting queries in this way leads to all sort of errors I don't find until runtime.

Any tips greatly appreiciated!

Ok after the tips below I now have this:

  db.execSQL("CREATE TABLE customer (_id INTEGER PRIMARY KEY NOT NULL, " 
                        + "phone_number INTEGER NOT NULL, "
                        + "name TEXT NOT NULL, "
                        + "email TEXT NOT NULL, "
                        + "address TEXT);");


    //get code
String q = "SELECT * FROM customer WHERE _id = " + customerDbId +";"
        Cursor mCursor = mDb.rawQuery(q, null);

        Customer customer = new Customer (mCursor);

in the Customer, I access the fields like this

mName = cursor.getString(2)

Ahh, I feel much better :)

Cheers Si

解决方案

Don't use model objects if you do not have to. I've concluded, unless there is significant business logic that can only be represented via model objects, that they are more trouble than they are worth in a mobile platform. Assuming you are stuck with model objects, have them load themselves out of a Cursor, rather than trying to pass in umpteen parameters. query() is much more verbose than rawQuery() for limited added value, if you know SQL. Assembling your CREATE TABLE clause via concatenation is self-imposed pain, not mandated by SQLite or Android. Don't use getColumnIndexOrThrow() from custom-written code. You wrote the query, so you know what order the columns are coming back in. Only use something like getColumnIndexOrThrow() if you are creating some abstract library that does not know the details of the Cursor it was given. String inherits from CharSequence, so all those casts can be dropped.