gpt4 book ai didi

javascript - ionicframework 使用 SQLite 进行 CRUD 操作

转载 作者:行者123 更新时间:2023-12-02 16:32:08 26 4
gpt4 key购买 nike

我已经添加了创建此示例应用程序所需的 ngcordova SQLite 插件

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>

<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">

<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->

<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>

<!-- cordova script (this will be a 404 during development) -->
<script src="js/ng-cordova.js"></script>
<script src="cordova.js"></script>

<!-- your app's js -->
<script src="js/app.js"></script>
</head>
<body ng-app="starter">

<ion-pane>

<ion-header-bar class="bar-stable">
<h1 class="title">Ionic Crud & SQLite</h1>
</ion-header-bar>

<ion-content ng-controller="AccountController">

<form ng-submit="addAccount()">
<div class="list">
<label class="item item-input item-stacked-label">
<span class="input-label">First Name</span>
<input type="text" placeholder="John" ng-model="firstnameText">
</label>
<label class="item item-input item-stacked-label">
<span class="input-label">Last Name</span>
<input type="text" placeholder="Suhr" ng-model="lastnameText">
</label>
<div class="padding">
<button class="button button-block button-positive">Create Account</button>
</div>
</div>
</form>

<ul class="list list-inset">
<li class="item item-divider">
{{accounts.length}} records
</li>
<li class="item" ng-repeat="account in accounts">
<i class="icon ion-person"></i>&nbsp; - &nbsp;
<span>{{account.firstname}} {{account.lastname}}</span>
</li>
</ul>

</ion-content>

</ion-pane>
</body>
</html>

app.js

var db = null;

angular.module('starter', ['ionic', 'ngCordova'])

.run(function($ionicPlatform, $cordovaSQLite) {
$ionicPlatform.ready(function() {

if(window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}

db = $cordovaSQLite.openDB({ name: "my.db" });

$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXIST people (id integer primary key, firstname text, lastname text)");
});
})

.controller('AccountController', function($scope, $cordovaSQLite) {

$scope.accounts = function() {
var query = "SELECT firstname, lastname FROM people";
$cordovaSQLite.execute(db, query);
}

$scope.addAccount = function(){
var query = "INSERT INTO people (firstname, lastname) VALUES (?, ?)";
$cordovaSQLite.execute(db, query, [$scope.firstnameText, $scope.lastnameText]);
$scope.firstnameText = '';
$scope.lastnameText = '';
}

});

我已经在我的设备上运行了我的应用程序,并且没有添加任何内容到列表中,这意味着我没有将任何内容保存到数据库中。有什么帮助吗?谢谢

最佳答案

我遇到了这个问题 - 经过一番研究后,我通过等待 Cordova 的 deviceready 解决了这个问题。加载 Angular 之前的事件。检查 API 文档 how to do a manual Angular initialisation

基本上你需要删除你的 ng-app指令,并调用 angular.bootstrap在以前 Cordova 的 deviceready 上的元素上事件已触发

我像这样添加了一个 delayedAngular.js 文件(不要忘记将其添加为 index.html 中的 <script>)

angular.element(document).ready(function() {
console.log("BOOTSTRAPPING...");
if (window.cordova) {
document.addEventListener('deviceready', function() {
console.log("window.cordova detected");
angular.bootstrap(document.body, ['myCoolApp']);
}, false);
} else {
console.log("window.cordova NOT detected");
angular.bootstrap(document.body, ['myCoolApp']);
}
});

在上面的代码中替换 myCoolApp与您的主应用程序模块的名称。我会尝试找到我发现此内容的博客文章以获取应有的信用。

我发现将其回退到 WebSQL 数据库以便在浏览器中进行测试非常有帮助,因为在设备上调试 SQLite 很痛苦。我在我的应用程序中使用了下面的代码 - 它使用 Angular promise ,因此请确保您熟悉它们(如果您想要警报,请确保还注入(inject) $window 。我还没有 root 我的手机,所以无法直接检查设备上的 SQLite 数据库:-/)

var initDB = function(dbName){

$log.log("Opening DB...");
var q = $q.defer();
var db;
if($cordovaSQLite && $window.sqlitePlugin !== undefined){
$window.alert("SQLite plugin detected");
db = $cordovaSQLite.openDB({ name: dbName });
q.resolve(db);
}
else {
db = $window.openDatabase(
dbName,
"0.0.1",
"My DB",
200000,
function(){
$window.alert("Created WebSQL DB!");
}
);
q.resolve(db);
}
return q.promise;
};

关于javascript - ionicframework 使用 SQLite 进行 CRUD 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28206834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com