iv_and/db_helper.dart
joe a8242c2a7e
Some checks are pending
Flutter CI / build (push) Waiting to run
feat: Invoiceアプリの基本機能を追加
Co-authored-by: aider (ollama_chat/7b) <aider@aider.chat>
2026-01-16 09:35:27 +09:00

63 lines
1.5 KiB
Dart

import 'package:sqflite/sqflite.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io' as io;
class DbHelper {
static Database? _database;
Future<Database> get database async {
if (_database != null) return _database!;
_database = await initDb();
return _database!;
}
initDb() async {
io.Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = '${documentsDirectory.path}/invoice.db';
var db = await openDatabase(path, version: 1, onCreate: _onCreate);
return db;
}
void _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE customers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
phone TEXT,
address TEXT,
email TEXT
)
''');
await db.execute('''
CREATE TABLE products (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
unit_price REAL,
discount REAL
)
''');
await db.execute('''
CREATE TABLE invoices (
id INTEGER PRIMARY KEY AUTOINCREMENT,
customer_id INTEGER,
date TEXT,
total REAL,
tax REAL,
discount_total REAL
)
''');
await db.execute('''
CREATE TABLE invoice_items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
invoice_id INTEGER,
product_id INTEGER,
quantity INTEGER,
unit_price REAL,
discount REAL
)
''');
}
}