JSFiddle - React, Tailwind, and code Playground
HTML
<div id='output'></div>
<script src="https://kripken.github.io/sql.js/js/sql.js"></script>
JavaScript
// This variable will receive instance of SQLite
var db = null;
// method taken from your demo
function print(text) {
var element = document.getElementById('output');
element.innerHTML += "<br/>" + text;//.replace(/\n/g, '<br>');
}
// method taken from your demo
// only one modification : returns true when everything ok, and false when an exception has been triggered
function execute(commands) {
try {
print( "<b>" + commands + "</b>" );
var data = db.exec(commands.replace(/\n/g, '; '));
print(JSON.stringify(data, null, ' '));
return true;
} catch(e) {
print(e);
return false;
}
}
var res = true;
for( i=0; i<10; i++ )
{
print( "Pass " + i );
var data = null;
// if a SQLite instance has already been opened, we just export its data,
// close it and reopen a new instance with the exported data
if( db != null )
{
data = db.export();
db.close();
db = null;
print( "Exporting data and closing sqlite, bytes=" + data.length );
}
if( data != null )
{
db = new SQL.Database( data );
}
else
{
db = new SQL.Database();
execute( "CREATE TABLE Persons( id INTEGER PRIMARY KEY AUTOINCREMENT, LastName varchar(255) NOT NULL );" );
}
execute( "SELECT count(*) as 'CountOfPersons' from Persons;" );
for( j=0; j<100; j++ )
{
res = execute( "insert into Persons (LastName) values ('Toto-"+i+"-"+j+"');" );
if( ! res )
break;
}
if( ! res )
break;
}
print( "Finished" );