如何用QT和SQLite实现文件操作

发布于 2024-12-12 17:02:24 字数 441 浏览 0 评论 0原文

Greetz,

我很好奇如何在 QT 和 SQLite 中使用基本文件操作,并且想知道是否有当前的示例。我的想法是这样的;

  • OnAppOpen - 在 :memory: 数据库中创建表(只需调用新文件)
  • OnFileSave - 将 :memory: 中的所有内容复制到磁盘上的文件中
  • OnFileOpen - 将磁盘上文件中的所有内容复制到 :memory:
  • OnFileNew - 删除当前 :memory: 数据库并创建一个新的(OnAppOpen)

或类似

  • AppOpen
  • FileNew - 与数据库断开连接并使用 :memory: name 创建新的
  • FileSave - 获取当前数据库名称

对此的任何帮助将不胜感激,谢谢。

Greetz,

I'm very curious on how to use basic file operations within QT and SQLite and wondering if there are current examples. My ideas are things like;

  • OnAppOpen - create tables in an :memory: database (just call new file)
  • OnFileSave - copy everything in :memory: into a file on disk
  • OnFileOpen - copy everything from file on disk to :memory:
  • OnFileNew - delete current :memory: database and Create a new one (OnAppOpen)

or something like

  • AppOpen
  • FileNew - disconnect from database and create new one with :memory: name
  • FileSave - get current database name

Any help with this would be more than appreciated, thx.

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

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

发布评论

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

评论(2

绝對不後悔。 2024-12-19 17:02:24

dbAdapter 示例(如果您想保存日志本身)。

public class DBAdapter {
    static final String ________________ (xtimes u need it)

    static final String DATABASE_CREATE = "CREATE TABLE ('_______');
    final Context context;
    DatabaseHelper DBHelper;
    SQLiteDatabase db;

    public DBAdapter(Context ctx)
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }
    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        @Override
        public void onCreate(SQLiteDatabase db)
        {
            try {
                db.execSQL(DATABASE_CREATE);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS vehicle");
            onCreate(db);
        }
    }

    public DBAdapter open() throws SQLException
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    public void close()
    {
        DBHelper.close();
    }

    public long insertVehicle(String _________ (xtimes u want))
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(_________);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    public Cursor getAllVehicles()
    {
        return db.query(DATABASE_TABLE, new String[] {DATABASE_ROW_ID, ____}, null, null, null, null, null);
    }

dbAdapter example if you want to save Logs per se.

public class DBAdapter {
    static final String ________________ (xtimes u need it)

    static final String DATABASE_CREATE = "CREATE TABLE ('_______');
    final Context context;
    DatabaseHelper DBHelper;
    SQLiteDatabase db;

    public DBAdapter(Context ctx)
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }
    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        @Override
        public void onCreate(SQLiteDatabase db)
        {
            try {
                db.execSQL(DATABASE_CREATE);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS vehicle");
            onCreate(db);
        }
    }

    public DBAdapter open() throws SQLException
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    public void close()
    {
        DBHelper.close();
    }

    public long insertVehicle(String _________ (xtimes u want))
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(_________);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    public Cursor getAllVehicles()
    {
        return db.query(DATABASE_TABLE, new String[] {DATABASE_ROW_ID, ____}, null, null, null, null, null);
    }
我的影子我的梦 2024-12-19 17:02:24

Qt 已经将 MySql 作为内置的 SQL 包用于数据库操作。由于许可证不兼容,QSQllite 可能不可用。如果你想使用sqllite,你应该安装使用sqllite服务的开发包。

像你提到的这样的操作绝对可以用 sqllite 完成。

您可以在 http://doc.trolltech.com/4.5/examples.html 找到 sqllite 示例#sql

Qt already has MySql as a built in SQL package for database operations. QSQllite may not be available due to license incompatibilities. If you want to use sqllite, you should install development package for using sqllite services.

Operations like u mentioned are absolutely doable with sqllite.

You can find sqllite examples at http://doc.trolltech.com/4.5/examples.html#sql

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