Issue
I follow the below instruction for using sqlLite with prepared DB file
I could setup in Xcode without any problem, but I can not import correctly the library in the JS file.
import SQLite from 'react-native-sqlite-storage';
console.log(SQLite) // {}
the SQLLite is an empty object. So, there is no the openDatabase method.
The library version is,
"react-native-sqlite-storage": "^6.0.1",
"react": "18.2.0",
"react-native": "0.71.7",
What I am doing wrong?
Solution
In the new version of docs, it's not updated but you can use like the below mention.
import { openDatabase } from 'react-native-sqlite-storage';
const createTable = ({ dbName, tableName, query }) => {
const db = openDatabase({ name: dbName });
db.transaction((txn) => {
txn.executeSql(
`SELECT name FROM sqlite_master WHERE type='table' AND name='${tableName}'`,
[],
(tx, result) => {
if (result?.rows?.length === 0) {
txn.executeSql(`DROP TABLE IF EXISTS ${tableName}`, []);
txn.executeSql(
query,
[]
);
console.log(`DataBase ${dbName} with table name ${tableName} is created successfully`);
} else {
console.log(`DataBase ${dbName} with table name ${tableName} is already exists`);
}
},
({ message }) => console.log('Error caught while creating table', message)
);
});
};
Then use the below function to insert the data
const insertDataInTable = ({ dbName, tableName, query, values }) => {
const db = openDatabase({ name: dbName });
db.transaction((txn) => {
txn.executeSql(
query,
values,
(tx, result) => {
console.log('result: ', result);
for (let i = 0; i < result?.rows?.length; ++i) {
console.log(`Data inserted into table '${tableName}' - `, result?.rows?.item(i));
}
},
({ message }) => console.log('Error caught while inserting data in table', message)
);
});
};
Answered By - amitpanday
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.