惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
A
About on SuperTechFans
H
Help Net Security
Engineering at Meta
Engineering at Meta
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
The Register - Security
The Register - Security
WordPress大学
WordPress大学
MongoDB | Blog
MongoDB | Blog
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
C
Check Point Blog
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
N
Netflix TechBlog - Medium
酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - Franky
I
InfoQ
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The GitHub Blog
The GitHub Blog
Last Week in AI
Last Week in AI
D
Docker
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 叶小钗
Jina AI
Jina AI
F
Fortinet All Blogs
宝玉的分享
宝玉的分享
小众软件
小众软件
有赞技术团队
有赞技术团队
F
Full Disclosure
月光博客
月光博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Recorded Future
Recorded Future
Apple Machine Learning Research
Apple Machine Learning Research
P
Proofpoint News Feed
量子位
U
Unit 42
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
V
V2EX
O
OpenAI News
S
Secure Thoughts
罗磊的独立博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Google Online Security Blog
Google Online Security Blog
Cloudbric
Cloudbric
W
WeLiveSecurity
IT之家
IT之家

博客园 - chuncn

Eslint 规则说明 磁盘阵列RAID原理、种类及性能优缺点对比 asp.net中调用命令行 JavaScript中的CSS属性对照表 webRTC-实时流媒体的福音 qt 5.1 quick vs qt 4.x quick for control zeroc ice的概念、组成与服务 国内外期货、外汇、股指期货 交易时间 使用Visual Leak Detector for Visual C++ 捕捉内存泄露 window8 metro 之 RSA Windows环境下使用Boost Qt 表格&列表数据驱动化(c++) 原创 Qt读写INI配置文件 Qt多国语言的实现与切换(国际化) Qt编码风格 -- 转 win32里玩事件-转 c++中捕捉内存泄露、异常 win32 http download win32 DirectUI控件开发与调用指南
sqlite in qt
chuncn · 2012-09-19 · via 博客园 - chuncn

storage.js

//storage.js

//数据库文件默认路径:QDeclarativeEngine::offlineStoragePath()
//数据库文件路径更改:QDeclarativeEngine::setOfflineStoragePath(const QString & dir)

// 打开并获取数据库对象
function getDatabase() {
     return openDatabaseSync("MyApp", "1.0", "StorageDatabase", 100000);
}
 
//初始化表
function initialize() {
    var db = getDatabase();
    db.transaction(
        function(tx) {
            tx.executeSql('CREATE TABLE IF NOT EXISTS myTable(name TEXT UNIQUE, study TEXT)');
	  });
}
 
//写入数据库
function setSetting(name, study) {
   var db = getDatabase();
   var res = "";
   db.transaction(function(tx) {
        var rs = tx.executeSql('INSERT OR REPLACE INTO myTable VALUES (?,?);', [name,study]);
              if (rs.rowsAffected > 0) {
                res = "OK";
              } else {
                res = "Error";
              }
        }
  );
  return res;
}

//获取数据1
function getSetting(name) {
   var res="";
   var db = getDatabase();
   db.transaction(function(tx) {
     var rs = tx.executeSql('SELECT name,study FROM myTable WHERE name=?;', [name]);
     if (rs.rows.length > 0) {
          res = rs.rows.item(0).name;
     } else {
         res = "Unknown";
     }
  })
  return res
}

//获取数据2
function getSetting2() {
  var r = "";
  var db = getDatabase();
  db.transaction(function(tx) {
    var rs = tx.executeSql('SELECT name,study FROM myTable');
    for(var i = 0; i < rs.rows.length; i++) {
        r += rs.rows.item(i).name + ", " + rs.rows.item(i).study + "\r\n"
    }
    print(r);
 })
 return r
}

main.qml

import QtQuick 1.1
import "storage.js" as Storage

Rectangle {
    width: 360
    height: 360
    id: screen
    Text {
        id: textDisplay
        anchors.centerIn: parent
    }
    Component.onCompleted: {
        // 初始化数据库
        Storage.initialize();
        // 赋值
        Storage.setSetting("张三","新疆大学");
        Storage.setSetting("李四","内蒙大学");
        //获取一个值,并把它写在textDisplay里
        //textDisplay.text = "The value of mySetting is:\n" + Storage.getSetting("mySetting");
        textDisplay.text = Storage.getSetting2();
    }
}