0% found this document useful (0 votes)
7 views

SQFLite_Overview

Uploaded by

fstbigo8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

SQFLite_Overview

Uploaded by

fstbigo8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Introduction to SQFLite

SQFLite is a popular SQLite plugin for Flutter that provides an easy-to-use interface for
interacting with SQLite databases on both Android and iOS platforms. It supports all
standard database operations, including CRUD (Create, Read, Update, Delete) operations.

### Key Features:


1. **Lightweight and Fast**: Suitable for local storage needs in mobile applications.
2. **Cross-Platform**: Works seamlessly on Android and iOS.
3. **Supports Complex Queries**: Allows the use of raw SQL queries for advanced database
manipulation.
4. **Transaction Support**: Ensures data integrity with support for transactions.

### Setting Up SQFLite:


To start using SQFLite, add the following dependencies in your `pubspec.yaml` file:

```
dependencies:
sqflite: ^2.2.8+4
path: ^1.8.3
```

### Example - CRUD Operations:


Here's a simple example of setting up a database and performing CRUD operations:

**1. Initialize Database**:


```dart
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';

Future<Database> openDB() async {


return openDatabase(
join(await getDatabasesPath(), 'demo.db'),
onCreate: (db, version) {
return db.execute(
'CREATE TABLE users(id INTEGER PRIMARY KEY, name TEXT, age INTEGER)',
);
},
version: 1,
);
}
```
**2. Insert Data**:
```dart
Future<void> insertUser(Database db, User user) async {
await db.insert('users', user.toMap());
}
```

**3. Read Data**:


```dart
Future<List<User>> getUsers(Database db) async {
final List<Map<String, dynamic>> maps = await db.query('users');
return List.generate(maps.length, (i) {
return User(
id: maps[i]['id'],
name: maps[i]['name'],
age: maps[i]['age'],
);
});
}
```

### Conclusion:
SQFLite is an excellent choice for Flutter developers who need reliable local storage for
their mobile applications. It combines the flexibility of SQL with the ease of use that Flutter
developers expect.

You might also like