asyncinitsqlite(){// Construct a StoreConfig objectconstSTORE_CONFIG:relationalStore.StoreConfig={name:'RdbTest.db',// Database file namesecurityLevel:relationalStore.SecurityLevel.S1,// Database security levelencrypt:false,// Optional parameter indicating whether the database is encryptedcustomDir:'customDir/subCustomDir',// Optional parameter for a custom database pathisReadOnly:false// Optional parameter indicating whether the database is read - only};// Check the database version and perform upgrade or downgrade operations if necessary// Default database version is 0, table structure: CLIENT_USER (ID INTEGER PRIMARY KEY AUTOINCREMENT, ACCOUNT TEXT NOT NULL, PASSWORD TEXT NOT NULL)constSQL_CREATE_TABLE='CREATE TABLE IF NOT EXISTS CLIENT_USER (ID INTEGER PRIMARY KEY AUTOINCREMENT, ACCOUNT TEXT NOT NULL, PASSWORD TEXT NOT NULL)';// CREATE TABLE SQL statement// Obtain a relevant RdbStore to operate the relational databaserelationalStore.getRdbStore(this.context,STORE_CONFIG,(err,store)=>{if (err){console.error(`Failed to get RdbStore. Code: ${err.code}, message: ${err.message}`);return;}console.info('Successfully obtained RdbStore.');// When the database is created, the default version is 0if (store.version===0){store.executeSql(SQL_CREATE_TABLE);// Create the data table// Set the database version to an integer greater than 0store.version=1;}// Assign the obtained RdbStore instance to the current rdbStore objectthis.rdbStore=store;});}
Inserting Data
addUserInfo(){if (this.account===undefined||this.account===''){console.info('Please enter an account');return;}if (this.password===undefined||this.password===''){console.info('Please enter a password');return;}// Use ArkData RDB to store user information in a table - based mannerif (this.rdbStore!==undefined){// Construct a ValuesBucket object required for the insert operationconstuserInfo:relationalStore.ValuesBucket={ACCOUNT:this.account,PASSWORD:this.password};// Insert data into the specified tablethis.rdbStore.insert('CLIENT_USER',userInfo,(err:BusinessError,rowId:number)=>{// If insertion is successful, return the row ID in the table; otherwise, return -1if (rowId===-1){console.error(`Failed to insert data. Code: ${err.code}, message: ${err.message}`);return;}console.info(`Data inserted successfully. Row ID: ${rowId}`);});}}
Querying Data
Querying All Data
queryUserInfoData(){if (this.rdbStore!==undefined){// Test querythis.rdbStore.querySql('SELECT ID, ACCOUNT, PASSWORD FROM CLIENT_USER ORDER BY ID DESC;',(err:BusinessError,resultSet)=>{if (err){console.error(`Failed to read the database table. Code: ${err.code}, message: ${err.message}`);return;}console.info(`Query result (object form): ${resultSet}`);// resultSet is a cursor over a collection of data, initially pointing to the -1st record, with valid data starting from 0.while (resultSet.goToNextRow()){constid=resultSet.getLong(resultSet.getColumnIndex('ID'));constaccount=resultSet.getString(resultSet.getColumnIndex('ACCOUNT'));constpasswd=resultSet.getString(resultSet.getColumnIndex('PASSWORD'));console.info(`id=${id}, account=${account}, passwd=${passwd}`);constuserInfo=newUserInfo(id,account,passwd);this.listUserInfo.push(userInfo);}// Release the memory of the datasetresultSet.close();});}}
Querying Specific Field Data
queryUserInfoByName(name:string){constpredicates2=newrelationalStore.RdbPredicates('CLIENT_USER');predicates2.equalTo('ACCOUNT',name);if (this.rdbStore!==undefined){this.rdbStore.query(predicates2,['ID','ACCOUNT','PASSWORD'],(err:BusinessError,resultSet)=>{if (err){console.error(`Failed to query data. Code: ${err.code}, message: ${err.message}`);return;}console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`);// resultSet is a cursor over a collection of data, initially pointing to the -1st record, with valid data starting from 0.while (resultSet.goToNextRow()){constid=resultSet.getLong(resultSet.getColumnIndex('ID'));constaccount=resultSet.getString(resultSet.getColumnIndex('ACCOUNT'));constpasswd=resultSet.getString(resultSet.getColumnIndex('PASSWORD'));console.info(`id=${id}, account=${account}, passwd=${passwd}`);constuserInfo=newUserInfo(id,account,passwd);this.listUserInfo.push(userInfo);}// Release the memory of the datasetresultSet.close();});}}
Top comments (0)