我知道你可以在 android 中使用 python 和其他脚本语言。但我还没有看到天气是否可以在android中使用python作为sqlite的接口。这可能吗?这是我第一个需要 sqlite 的 Android 应用程序,并且使用 java api 的速度很慢。

如果这是不可能的,有人可以给我指点一个关于 android 中 sqlite 的好教程吗?我找到了很多,但它们都完全不同,我完全不知道哪一个是最好的方法。

很难想象 google 希望你如何使用 sqlite 数据库。看起来您需要大约 10 个不同的类来查询数据库。

有帮助吗?

解决方案

实际上你只需要3个类:

A 内容提供商, ,如下所示: http://developer.android.com/guide/topics/providers/content-providers.html

第二个你需要的是 SQLiteOpenHelper 最后但并非最不重要的一个 光标

编辑:只是注意到从片段中并不明显 db 变量是.它是 SQLiteOpenHelper 或更好的我的扩展(我只重写了 onCreate、onUpgrade 和构造函数。见下文^^

ContentProvider 将与数据库通信并执行插入、更新和删除操作。内容提供者还将允许代码的其他部分(甚至其他应用程序,如果您允许的话)访问存储在 sqlite 中的数据。

然后,您可以覆盖插入/删除/查询/更新函数并向其中添加您的功能,例如根据意图的 URI 执行不同的操作。

public int delete(Uri uri, String whereClause, String[] whereArgs) {
    int count = 0;

    switch(URI_MATCHER.match(uri)){
    case ITEMS:
        // uri = content://com.yourname.yourapp.Items/item
        // delete all rows
        count = db.delete(TABLE_ITEMS, whereClause, whereArgs);
        break;
    case ITEMS_ID:
        // uri = content://com.yourname.yourapp.Items/item/2
        // delete the row with the id 2
        String segment = uri.getPathSegments().get(1);
        count = db.delete(TABLE_ITEMS, 
                Item.KEY_ITEM_ID +"="+segment
                +(!TextUtils.isEmpty(whereClause)?" AND ("+whereClause+")":""),
                whereArgs);
        break;
    default:
        throw new IllegalArgumentException("Unknown Uri: "+uri);
    }

    return count;
}

UriMatcher 定义为

private static final int ITEMS = 1;
private static final int ITEMS_ID = 2;
private static final String AUTHORITY_ITEMS ="com.yourname.yourapp.Items";
private static final UriMatcher URI_MATCHER;

static {
    URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
    URI_MATCHER.addURI(AUTHORITY_ITEMS, "item", ITEMS);
    URI_MATCHER.addURI(AUTHORITY_ITEMS, "item/#", ITEMS_ID);
}

这样您就可以决定是否只返回或更新 1 个结果,或者是否应查询所有结果。

SQLiteOpenHelper 将实际执行插入,并且如果 SQLite 数据库的结构发生变化,也会负责升级,您可以通过覆盖来执行它

class ItemDatabaseHelper extends SQLiteOpenHelper {
    public ItemDatabaseHelper(Context context){
        super(context, "myDatabase.db", null, ITEMDATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String createItemsTable = "create table " + TABLE_ITEMS + " (" +
            ...
        ");";

        // Begin Transaction
        db.beginTransaction();
        try{
            // Create Items table
            db.execSQL(createItemsTable);

            // Transaction was successful
            db.setTransactionSuccessful();
        } catch(Exception ex) {
            Log.e(this.getClass().getName(), ex.getMessage(), ex);
        } finally {
            // End transaction
            db.endTransaction();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String dropItemsTable = "DROP TABLE IF EXISTS " + TABLE_ITEMS;

        // Begin transaction
        db.beginTransaction();

        try {
            if(oldVersion<2){
                // Upgrade from version 1 to version 2: DROP the whole table
                db.execSQL(dropItemsTable);
                onCreate(db);
                Log.i(this.getClass().toString(),"Successfully upgraded to Version 2");
            }
            if(oldVersion<3) {
                // minor change, perform an ALTER query
                db.execSQL("ALTER ...");
            }

            db.setTransactionSuccessful();
        } catch(Exception ex){
            Log.e(this.getClass().getName(), ex.getMessage(), ex);
        } finally {
            // Ends transaction
            // If there was an error, the database won't be altered
            db.endTransaction();
        }
    }
}

然后是最简单的部分:执行查询:

String[] rows = new String[] {"_ID", "_name", "_email" };
Uri uri = Uri.parse("content://com.yourname.yourapp.Items/item/2";

// Alternatively you can also use getContentResolver().insert/update/query/delete methods
Cursor c = managedQuery(uri, rows, "someRow=1", null, null); 

据我所知,这基本上是最优雅的方式。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top