SQLite 插入循环从 Excel 文件到 SQLite 数据库
SQLite Insert Looping from Excel File to SQLite Database
首先,我是 SQLite 的新手。
我尝试制作一个 AIR 应用程序,它可以将 excel 文件中的值导入 SQLite 数据库。我尝试做的是创建一个循环,允许我将数据从 excel 插入到 SQLite 数据库中。下面是我的代码;
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.SQLEvent;
import flash.events.TimerEvent;
import flash.data.SQLStatement;
import flash.data.SQLConnection;
import flash.utils.Timer;
import flash.filesystem.File;
import flash.net.FileFilter;
import fl.controls.Button;
import fl.controls.DataGrid;
import fl.controls.ScrollPolicy;
import lib.xlsxreader.Worksheet;
import lib.xlsxreader.XLSXLoader;
public class DataTransfer extends MovieClip
{
private var i;
private var fileGet: File = new File();
private var dbFile: File = new File();
private var excel_loader: XLSXLoader = new XLSXLoader();
private var sqlc: SQLConnection = new SQLConnection();
private var sqls: SQLStatement = new SQLStatement();
public function DataTransfer()
{
var btnImport: Button = new Button();
btnImport.x = btnImport.y = 10;
btnImport.label = "Select Excel File";
btnImport.addEventListener(MouseEvent.CLICK, selectExcelFile);
addChild(btnImport);
fileGet = File.applicationDirectory;
fileGet.addEventListener(Event.CANCEL, fileCancel);
fileGet.addEventListener(Event.SELECT, fileSelect);
dbFile = fileGet.resolvePath("test.sqlite");
excel_loader.addEventListener(Event.COMPLETE, loadingComplete);
sqlc.addEventListener(SQLEvent.OPEN, db_opened);
sqlc.openAsync(dbFile);
}
private function selectExcelFile(e: MouseEvent): void
{
var fileFilter = new FileFilter("Select Excel File", "*.xls;*.xlsx;*.xlsm");
fileGet.browseForOpen("Select Excel File", [fileFilter]);
}
private function fileCancel(e: Event): void
{
trace("Cancel");
}
private function fileSelect(e: Event): void
{
trace("File Path is : " + fileGet.nativePath);
excel_loader.load(fileGet.nativePath);
}
private function loadingComplete(e: Event): void
{
var row: int = new int();
var sheet_2: Worksheet = excel_loader.worksheet("Sheet2");
var arColumn1: Array = new Array();
var arColumn2: Array = new Array();
var arColumn3: Array = new Array();
row = sheet_2.rows;
sqls.text = ""
for (i = 1; i <= row; i++)
{
arColumn1[i - 1] = sheet_2.getCellValue("A" + i);
arColumn2[i - 1] = sheet_2.getCellValue("B" + i);
arColumn3[i - 1] = sheet_2.getCellValue("C" + i);
sqls.text = sqls.text + "INSERT INTO test_table (col1) VALUES('" + arColumn1[i - 1] + "');";
}
trace(sqls.text);
sqls.execute();
refresh();
var dtExcel: DataGrid = new DataGrid();
dtExcel.x = 10;
dtExcel.y = 40;
dtExcel.width = 300;
dtExcel.height = 100;
dtExcel.horizontalScrollPolicy = ScrollPolicy.ON;
dtExcel.verticalScrollPolicy = ScrollPolicy.ON;
var col1 = dtExcel.addColumn("Column1");
var col2 = dtExcel.addColumn("Column2");
var col3 = dtExcel.addColumn("Column3");
col1.minWidth = 100;
col2.minWidth = 200;
col3.minWidth = 300;
for (i = 0; i <= row - 1; i++)
{
dtExcel.addItem(
{
Column1: arColumn1[i],
Column2: arColumn2[i],
Column3: arColumn3[i]
});
}
addChild(dtExcel);
}
private function db_opened(e: SQLEvent): void
{
sqls.sqlConnection = sqlc;
sqls.text = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY AUTOINCREMENT, col1 TEXT, col2 TEXT, col3 TEXT)";
sqls.execute();
refresh();
}
private function refresh(e: TimerEvent = null): void
{
var timer: Timer = new Timer(10, 1);
timer.addEventListener(TimerEvent.TIMER, refresh);
if (!sqls.executing)
{
sqls.text = "SELECT * FROM test_table";
sqls.execute();
}
else
{
timer.start();
}
}
}
}
我在这行有问题;
for (i = 1; i <= row; i++)
{
arColumn1[i - 1] = sheet_2.getCellValue("A" + i);
arColumn2[i - 1] = sheet_2.getCellValue("B" + i);
arColumn3[i - 1] = sheet_2.getCellValue("C" + i);
sqls.text = sqls.text + "INSERT INTO test_table (col1) VALUES('" + arColumn1[i - 1] + "');";
}
我尝试循环 sqls.text
以便 excel 值可以传递到 SQLite 数据库中。但是 sqls.execute()
只执行第一个 INSERT
.
我尝试使用 trace(sqls.text)
查看 sqls.text
的值,我认为我的 SQL 声明是正确的(假设 SQLite 使用 PostgreSQL语法)如下图;
INSERT INTO test_table (col1) VALUES('saya');INSERT INTO test_table (col1) VALUES('makan');INSERT INTO test_table (col1) VALUES('minum');INSERT INTO test_table (col1) VALUES('');INSERT INTO test_table (col1) VALUES('');INSERT INTO test_table (col1) VALUES('main');
我假设我的语法是正确的,因为我在 https://rextester.com/l/postgresql_online_compiler 中使用了上面的语法并且它工作得很好。
我假设我的循环实现是正确的,因为我的数据网格 dtExcel
在正确的列和行中显示了完全正确的值。
我也尝试在循环中使用 sqls.execute()
但出现以下错误:
Error: Error #3106: Property cannot be changed while SQLStatement.executing is true.
有没有办法做这个循环?或者我需要使用开源 AS3 SQLite 库来做到这一点?
编辑:
根据here,我可以把上面的SQLite语法改成;
INSERT INTO test_table (col1) VALUES('saya'),('makan'),('minum'),(''),(''),('main');
然而,这仍然会出现以下错误;
Error #2044: Unhandled SQLErrorEvent:. errorID=3115, operation=execute
, message=Error #3115: SQL Error. , details=near ',': syntax error
尝试在每个循环中执行一个命令,并在准备好的语句中执行和参数:
for (i = 1; i <= row; i++)
{
arColumn1[i - 1] = sheet_2.getCellValue("A" + i);
arColumn2[i - 1] = sheet_2.getCellValue("B" + i);
arColumn3[i - 1] = sheet_2.getCellValue("C" + i);
sqls.clearParameters();
sqls.text = "INSERT INTO test_table (col1, col2, col3) VALUES(:param1, :param2, :param3)";
sqls.parameters[":param1"] = arColumn1[i - 1];
sqls.parameters[":param2"] = arColumn2[i - 1];
sqls.parameters[":param3"] = arColumn3[i - 1];
sqls.execute();
}
此外,您可以使用交易:
sqls.begin();
- for loop here
sqls.commit();
首先,我是 SQLite 的新手。
我尝试制作一个 AIR 应用程序,它可以将 excel 文件中的值导入 SQLite 数据库。我尝试做的是创建一个循环,允许我将数据从 excel 插入到 SQLite 数据库中。下面是我的代码;
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.SQLEvent;
import flash.events.TimerEvent;
import flash.data.SQLStatement;
import flash.data.SQLConnection;
import flash.utils.Timer;
import flash.filesystem.File;
import flash.net.FileFilter;
import fl.controls.Button;
import fl.controls.DataGrid;
import fl.controls.ScrollPolicy;
import lib.xlsxreader.Worksheet;
import lib.xlsxreader.XLSXLoader;
public class DataTransfer extends MovieClip
{
private var i;
private var fileGet: File = new File();
private var dbFile: File = new File();
private var excel_loader: XLSXLoader = new XLSXLoader();
private var sqlc: SQLConnection = new SQLConnection();
private var sqls: SQLStatement = new SQLStatement();
public function DataTransfer()
{
var btnImport: Button = new Button();
btnImport.x = btnImport.y = 10;
btnImport.label = "Select Excel File";
btnImport.addEventListener(MouseEvent.CLICK, selectExcelFile);
addChild(btnImport);
fileGet = File.applicationDirectory;
fileGet.addEventListener(Event.CANCEL, fileCancel);
fileGet.addEventListener(Event.SELECT, fileSelect);
dbFile = fileGet.resolvePath("test.sqlite");
excel_loader.addEventListener(Event.COMPLETE, loadingComplete);
sqlc.addEventListener(SQLEvent.OPEN, db_opened);
sqlc.openAsync(dbFile);
}
private function selectExcelFile(e: MouseEvent): void
{
var fileFilter = new FileFilter("Select Excel File", "*.xls;*.xlsx;*.xlsm");
fileGet.browseForOpen("Select Excel File", [fileFilter]);
}
private function fileCancel(e: Event): void
{
trace("Cancel");
}
private function fileSelect(e: Event): void
{
trace("File Path is : " + fileGet.nativePath);
excel_loader.load(fileGet.nativePath);
}
private function loadingComplete(e: Event): void
{
var row: int = new int();
var sheet_2: Worksheet = excel_loader.worksheet("Sheet2");
var arColumn1: Array = new Array();
var arColumn2: Array = new Array();
var arColumn3: Array = new Array();
row = sheet_2.rows;
sqls.text = ""
for (i = 1; i <= row; i++)
{
arColumn1[i - 1] = sheet_2.getCellValue("A" + i);
arColumn2[i - 1] = sheet_2.getCellValue("B" + i);
arColumn3[i - 1] = sheet_2.getCellValue("C" + i);
sqls.text = sqls.text + "INSERT INTO test_table (col1) VALUES('" + arColumn1[i - 1] + "');";
}
trace(sqls.text);
sqls.execute();
refresh();
var dtExcel: DataGrid = new DataGrid();
dtExcel.x = 10;
dtExcel.y = 40;
dtExcel.width = 300;
dtExcel.height = 100;
dtExcel.horizontalScrollPolicy = ScrollPolicy.ON;
dtExcel.verticalScrollPolicy = ScrollPolicy.ON;
var col1 = dtExcel.addColumn("Column1");
var col2 = dtExcel.addColumn("Column2");
var col3 = dtExcel.addColumn("Column3");
col1.minWidth = 100;
col2.minWidth = 200;
col3.minWidth = 300;
for (i = 0; i <= row - 1; i++)
{
dtExcel.addItem(
{
Column1: arColumn1[i],
Column2: arColumn2[i],
Column3: arColumn3[i]
});
}
addChild(dtExcel);
}
private function db_opened(e: SQLEvent): void
{
sqls.sqlConnection = sqlc;
sqls.text = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY AUTOINCREMENT, col1 TEXT, col2 TEXT, col3 TEXT)";
sqls.execute();
refresh();
}
private function refresh(e: TimerEvent = null): void
{
var timer: Timer = new Timer(10, 1);
timer.addEventListener(TimerEvent.TIMER, refresh);
if (!sqls.executing)
{
sqls.text = "SELECT * FROM test_table";
sqls.execute();
}
else
{
timer.start();
}
}
}
}
我在这行有问题;
for (i = 1; i <= row; i++)
{
arColumn1[i - 1] = sheet_2.getCellValue("A" + i);
arColumn2[i - 1] = sheet_2.getCellValue("B" + i);
arColumn3[i - 1] = sheet_2.getCellValue("C" + i);
sqls.text = sqls.text + "INSERT INTO test_table (col1) VALUES('" + arColumn1[i - 1] + "');";
}
我尝试循环 sqls.text
以便 excel 值可以传递到 SQLite 数据库中。但是 sqls.execute()
只执行第一个 INSERT
.
我尝试使用 trace(sqls.text)
查看 sqls.text
的值,我认为我的 SQL 声明是正确的(假设 SQLite 使用 PostgreSQL语法)如下图;
INSERT INTO test_table (col1) VALUES('saya');INSERT INTO test_table (col1) VALUES('makan');INSERT INTO test_table (col1) VALUES('minum');INSERT INTO test_table (col1) VALUES('');INSERT INTO test_table (col1) VALUES('');INSERT INTO test_table (col1) VALUES('main');
我假设我的语法是正确的,因为我在 https://rextester.com/l/postgresql_online_compiler 中使用了上面的语法并且它工作得很好。
我假设我的循环实现是正确的,因为我的数据网格 dtExcel
在正确的列和行中显示了完全正确的值。
我也尝试在循环中使用 sqls.execute()
但出现以下错误:
Error: Error #3106: Property cannot be changed while SQLStatement.executing is true.
有没有办法做这个循环?或者我需要使用开源 AS3 SQLite 库来做到这一点?
编辑:
根据here,我可以把上面的SQLite语法改成;
INSERT INTO test_table (col1) VALUES('saya'),('makan'),('minum'),(''),(''),('main');
然而,这仍然会出现以下错误;
Error #2044: Unhandled SQLErrorEvent:. errorID=3115, operation=execute , message=Error #3115: SQL Error. , details=near ',': syntax error
尝试在每个循环中执行一个命令,并在准备好的语句中执行和参数:
for (i = 1; i <= row; i++)
{
arColumn1[i - 1] = sheet_2.getCellValue("A" + i);
arColumn2[i - 1] = sheet_2.getCellValue("B" + i);
arColumn3[i - 1] = sheet_2.getCellValue("C" + i);
sqls.clearParameters();
sqls.text = "INSERT INTO test_table (col1, col2, col3) VALUES(:param1, :param2, :param3)";
sqls.parameters[":param1"] = arColumn1[i - 1];
sqls.parameters[":param2"] = arColumn2[i - 1];
sqls.parameters[":param3"] = arColumn3[i - 1];
sqls.execute();
}
此外,您可以使用交易:
sqls.begin();
- for loop here
sqls.commit();