Android 内容提供商

发布于 2024-09-26 08:15:16 字数 110 浏览 5 评论 0原文

我想获取创建数据库的内容提供商的代码。我正在使用位于此处的工具 tools/sqllite3.exe 来检查数据库是否已创建。

请让我知道这件事的逐步程序...

谢谢, -D

I want to get a code of content provider which a database is created. I am using the tool which located here tools/sqllite3.exe to check if the database is created.

Please let me know the step by step procedure for this thing ...

Thanks,
-D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

多孤肩上扛 2024-10-03 08:15:16

您不使用 ContentProvider 来创建数据库,而是使用 SQLiteOpenHelper 类来创建数据库。至少这是更好的方法,

class MyDatabase extends SQLiteOpenHelper {
    public MyDatabase(Context context, dbName, null, version){
        super(context, dbName, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String createItemsTable = "create table mytable ("+
            "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "_title TEXT NOT NULL, " +
            "_subtitle TEXT NULL " +
        " );";

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

            // Transaction was successful
            db.setTransactionSuccessful();
        } catch(SQLException 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 mytable";

        // Begin transaction
        db.beginTransaction();

        try {
            if(oldVersion<2){
                // Do your update/alter code here
            }

            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();
        }
    }
}

他们只需使用实例化您的助手

MyDatabase myDb = new MyDatabase(getContext(),"databasename.db", null, 1); 

然后助手将创建数据库(如果不存在),如果旧版本存在则升级它,或者如果存在并且版本匹配则简单地打开它

You don't crate the database with the ContentProvider, but with the SQLiteOpenHelper class. At least that's the better way of doing it

class MyDatabase extends SQLiteOpenHelper {
    public MyDatabase(Context context, dbName, null, version){
        super(context, dbName, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        String createItemsTable = "create table mytable ("+
            "_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "_title TEXT NOT NULL, " +
            "_subtitle TEXT NULL " +
        " );";

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

            // Transaction was successful
            db.setTransactionSuccessful();
        } catch(SQLException 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 mytable";

        // Begin transaction
        db.beginTransaction();

        try {
            if(oldVersion<2){
                // Do your update/alter code here
            }

            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();
        }
    }
}

them simply instantiate your helper with

MyDatabase myDb = new MyDatabase(getContext(),"databasename.db", null, 1); 

The helper will then create the database, if it doesn't exist, upgrade it if an older version exists or simply open it if it exists and the version matches

岛歌少女 2024-10-03 08:15:16

我使用了本教程

http://www.devx.com/wireless/Article /41133/1763/page/2

我现在有一个很好的内容提供程序,它允许我轻松查询表,并且比其他数据库灵活得多。

I used this tutorial

http://www.devx.com/wireless/Article/41133/1763/page/2

I now have a nice content provider that allows me to easily query the table and is much more flexible than the other databases out there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文