Reputation: 303
In version 33 of the expo sdk, sqlite got moved to it's own package, expo-sqlite, and now I can't get the types to load.
Instead of
import {SQLite} from 'expo';
I have
import {SQLite} from 'expo-sqlite';
But the types are not getting loaded.
I get the following type error:
Cannot find namespace 'SQLite'.
Upvotes: 2
Views: 2797
Reputation: 129
Run below command to install:
npx expo install expo-sqlite
And import in your project:
import * as SQLite from 'expo-sqlite';
const db = SQLite.openDatabase('DBName.db');
Setup database:
const setupDatabaseAsync = async () => {
return new Promise((resolve, reject) => {
db.transaction((tx) => {
tx.executeSql(
'CREATE TABLE IF NOT EXISTS table_name (id INTEGER PRIMARY KEY NOT NULL, name TEXT);',
[],
(tx, success) => {
resolve(success);
console.log('SUCCESS IN SETUP DB');
},
(err) => {
reject(err);
console.log('ERROR IN SETUP DB');
}
);
});
});
}
Alternatively, you can use expo-sqlite-orm if you don't want to write queries.
Upvotes: -2
Reputation: 94
If your problem is accessing the data from a SQLResultSetRowList, then I would suggest you to define an interface like CustomResultset that extends SQLResultSetRowList.
For example:
import * as SQLite from 'expo-sqlite';
interface CustomResultSet extends SQLite.SQLResultSetRowList {
_array: [],
}
This may not be the optimal solution, but you should be able to access the Data through the _array property without having type errors in your code.
Upvotes: 0
Reputation: 303
Types were missing in expo-sqlite package. I opened up an issue on Expo's GitHub page, and the problem has been resolved.
GitHub issue: https://github.com/expo/expo/issues/5264
Github pull request: https://github.com/expo/expo/pull/5544
Upvotes: 1
Reputation: 2365
I encountered the same issue and created a Type Declaration file which solved the issue for me.
https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html
I copied the declarations from this package https://www.npmjs.com/package/@types/expo
This is the declaration https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/expo/v31/index.d.ts#L2467
My project now has an expo-sqlite.d.ts
file in the src
folder which looks like
declare module 'expo-sqlite' {
export namespace SQLite {
type Error = any;
interface Database {
transaction(
callback: (transaction: Transaction) => any,
error?: (error: Error) => any, // TODO def of error
success?: () => any
): void;
}
interface Transaction {
executeSql(
sqlStatement: string,
arguments?: string[] | number[],
success?: (transaction: Transaction, resultSet: ResultSet) => any,
error?: (transaction: Transaction, error: Error) => any
): void;
}
interface ResultSet {
insertId: number;
rowAffected: number;
rows: {
length: number;
item: (index: number) => any;
_array: HashMap[];
};
}
function openDatabase(
name:
| string
| {
name: string;
version?: string;
description?: string;
size?: number;
callback?: () => any;
},
version?: string,
description?: string,
size?: number,
callback?: () => any
): any;
}
}
Upvotes: 0
Reputation: 13916
You don't seem to have downloaded the module. Install the module and apply it.
npm install expo-sqlite
Upvotes: 1