By registering with us, you'll be able to discuss, share and private message with other members of our community.
SignUp Now!<?php
// Path to your current database
$old_db_path = 'api/.db.db';
// Create a new database file for the export
$new_db_path = 'api/new.db.db';
try {
// Open the old database
$old_db = new SQLite3($old_db_path);
// Open the new database for export
$new_db = new SQLite3($new_db_path);
// Begin transaction in new DB
$new_db->exec('BEGIN TRANSACTION');
// Get the list of all tables
$tables = $old_db->query("SELECT name FROM sqlite_master WHERE type='table';");
while ($table = $tables->fetchArray(SQLITE3_ASSOC)) {
$table_name = $table['name'];
// Get table schema
$schema = $old_db->querySingle("SELECT sql FROM sqlite_master WHERE name='$table_name'");
$new_db->exec($schema);
// Copy table data
$data = $old_db->query("SELECT * FROM $table_name");
while ($row = $data->fetchArray(SQLITE3_ASSOC)) {
$columns = implode(", ", array_keys($row));
$values = implode(", ", array_map(function($value) { return "'" . SQLite3::escapeString($value) . "'"; }, $row));
$new_db->exec("INSERT INTO $table_name ($columns) VALUES ($values)");
}
}
// Commit the transaction
$new_db->exec('COMMIT');
// Close both databases
$old_db->close();
$new_db->close();
echo "Database exported successfully to new.db.db.";
} catch (Exception $e) {
// Handle errors
error_log("Database export error: " . $e->getMessage());
echo "Failed to export database.";
}
?>