Android 使用IONIC 3+在SQLite中创建数据库;科尔多瓦
我正在用SQLite开发一个应用程序,创建并列出用户 为此,我创建了一个服务,在其中我创建了数据库和两个方法(create和list),应该提到的是,没有错误,我已经正确导入了所有内容 database.ts:Android 使用IONIC 3+在SQLite中创建数据库;科尔多瓦,android,sqlite,cordova,ionic-framework,android-sqlite,Android,Sqlite,Cordova,Ionic Framework,Android Sqlite,我正在用SQLite开发一个应用程序,创建并列出用户 为此,我创建了一个服务,在其中我创建了数据库和两个方法(create和list),应该提到的是,没有错误,我已经正确导入了所有内容 database.ts: @Injectable() export class DatabaseProvider { private db: SQLiteObject; private isOpen: boolean; constructor( public http: Http,
@Injectable()
export class DatabaseProvider {
private db: SQLiteObject;
private isOpen: boolean;
constructor(
public http: Http,
public storage: SQLite) {
if(!this.isOpen){
this.storage = new SQLite();
this.storage.create({ name: "data.db", location:"default" }).then((db:SQLiteObject) => {
this.db = db;
db.executeSql("CREATE TABLE IF NOT EXIST usuarios (Codigo INTEGER PRIMARY KEY AUTOINCREMENT, Identificacion INTEGER, Nombre TEXT, Apellido TEXT)", []);
this.isOpen = true;
}).catch((error) => {
console.log(error);
})
}
}
crearUsuario(Identificacion:number, Nombre:string, Apellido:string){
return new Promise ((resolve , reject) => {
let sql = "INSERT INTO usuarios (Identificacion, Nombre, Apellido) VALUES (?, ?, ?)";
this.db.executeSql(sql, [Identificacion, Nombre, Apellido]).then((data) => {
resolve(data);
}, (error) => {
reject(error);
});
});
}
listarUsuario(){
return new Promise ((resolve , reject) => {
this.db.executeSql("SELECT * FROM usuarios", []).then((data) => {
let arrayUsuarios = [];
if(data.rows.length > 0){
for (var i=0; i < data.rows.length; i++){
arrayUsuarios.push({
Codigo: data.rows.item(i).Codigo,
Identificacion: data.rows.item(i).Identificacion,
Nombre: data.rows.item(i).Nombre,
Apellido: data.rows.item(i).Apellido,
});
}
}
resolve(arrayUsuarios);
}, (error) => {
reject(error);
})
});
}
}
我正在物理设备上测试我的应用程序,但它什么都不做。。。不要创建,不要列出用户,我想我不是在创建数据库,我是爱奥尼亚3的新手,要在物理设备上部署我的应用程序,首先我使用命令爱奥尼亚cordova build android
,然后使用命令爱奥尼亚cordova run android--device
我做错了什么?如何通过在物理设备上尝试我的应用程序来发现错误?用我的调试控制台?Ionic 3数据库实际存储在我的设备(目录)中的什么位置?有什么帮助吗?我认为您的问题在于以下语法:-
CREATE TABLE IF NOT EXIST usuarios (Codigo INTEGER PRIMARY KEY AUTOINCREMENT, Identificacion INTEGER, Nombre TEXT, Apellido TEXT)
这是错误的
也就是说,关键字是存在
,而不是存在
。我建议尝试/使用:-
db.executeSql("CREATE TABLE IF NOT EXISTS usuarios (Codigo INTEGER PRIMARY KEY AUTOINCREMENT, Identificacion INTEGER, Nombre TEXT, Apellido TEXT)", []);
要在sqlite中创建表,首先需要创建存储。您可以这样做:
if (this.platform.is('cordova'))
this.sqlite.create({
name: 'fundamentos.offline',
location: 'default'
})
.then((db: SQLiteObject) => {
db.executeSql(CREATE TABLE IF NOT EXIST usuarios
(Codigo INTEGER PRIMARY KEY AUTOINCREMENT, Identificacion INTEGER,
Nombre TEXT, Apellido TEXT),[])
.then(() => console.log('Table Created'))
.catch(e => {
console.error(JSON.stringify(e));
});`
供参考:
检查此SQLite积垢操作示例应用程序
db.executeSql("CREATE TABLE IF NOT EXISTS usuarios (Codigo INTEGER PRIMARY KEY AUTOINCREMENT, Identificacion INTEGER, Nombre TEXT, Apellido TEXT)", []);
if (this.platform.is('cordova'))
this.sqlite.create({
name: 'fundamentos.offline',
location: 'default'
})
.then((db: SQLiteObject) => {
db.executeSql(CREATE TABLE IF NOT EXIST usuarios
(Codigo INTEGER PRIMARY KEY AUTOINCREMENT, Identificacion INTEGER,
Nombre TEXT, Apellido TEXT),[])
.then(() => console.log('Table Created'))
.catch(e => {
console.error(JSON.stringify(e));
});`