如何将sql数据导入sqlite?

时间:2023-01-13 18:01:36

I have an iPad application that suppose to be able to work offline. I got a local database(sqlite) that stored all the data needed. When i want to update the database, i create my query from the server side (PHP server), make it as a sql file, download to the app, and tried to read the sql file. Below is the sample code

我有一个iPad应用程序,假设能够脱机工作。我有一个本地数据库(sqlite)存储了所需的所有数据。当我想更新数据库时,我从服务器端(PHP服务器)创建我的查询,将其作为sql文件,下载到应用程序,并尝试读取sql文件。下面是示例代码

$txt = "CREATE TABLE IF NOT EXISTS `UOM` (`uom_id` int(11), `uom_name` varchar(200), `uom_status` int(11));";

$txt .= "INSERT INTO `UOM` (`uom_id`, `uom_name`, `uom_status`) VALUES (1, 'Unit', 1);";
$txt .= "INSERT INTO `UOM` (`uom_id`, `uom_name`, `uom_status`) VALUES (2, 'Kg', 1);";
$txt .= "INSERT INTO `UOM` (`uom_id`, `uom_name`, `uom_status`) VALUES (3, 'Liter', 1);";

$myfile = fopen("queries_" . $timeStamp . ".sql", "w");
fwrite($myfile, $txt);
fclose($myfile);

My question is, how can i import it into my local database ?i cannot open the sql file and read the query line by line since the file is too large(my database contain more than 40k of records), when I try to read it line by line (inside a loop) the app crash due to memory problem (very tight loop). I would like to know is there any way to execute this sql file on my local database without reading the file ?something like import this sql file to my local database programatically.

我的问题是,如何将其导入我的本地数据库?我无法打开sql文件并逐行读取查询,因为文件太大(我的数据库包含超过40k的记录),当我尝试读取它时逐行(在循环内)由于内存问题(非常紧的循环)应用程序崩溃。我想知道有没有办法在我的本地数据库上执行这个sql文件而不读取文件?就像以编程方式将这个sql文件导入我的本地数据库。

Thanks.

2 个解决方案

#1


Well what you need is a service to return you subset of data like paging. And then load one by one each pages. Its lengthier process but its more memory efficient.

那么你需要的是一种服务来返回你的数据子集,如分页。然后逐页加载。它的过程较长,但内存效率更高。

#2


I think the best way is create synch your db with remote db using get or post request that producing JSON format.

我认为最好的方法是使用生成JSON格式的get或post请求使用远程数据库创建同步数据库。

For example : GET
http://your....website.com/request/updateData.php?page=1&lastUpdate=342349218312312

例如:GET http://your....website.com/request/updateData.php?page = 1&lastUpdate = 342349218312312

Where you passing the page number and the timestamp for your local db.

您传递本地数据库的页码和时间戳的位置。

Your Json response with number of page and you custom data.

您的Json响应与页数和您的自定义数据。

#1


Well what you need is a service to return you subset of data like paging. And then load one by one each pages. Its lengthier process but its more memory efficient.

那么你需要的是一种服务来返回你的数据子集,如分页。然后逐页加载。它的过程较长,但内存效率更高。

#2


I think the best way is create synch your db with remote db using get or post request that producing JSON format.

我认为最好的方法是使用生成JSON格式的get或post请求使用远程数据库创建同步数据库。

For example : GET
http://your....website.com/request/updateData.php?page=1&lastUpdate=342349218312312

例如:GET http://your....website.com/request/updateData.php?page = 1&lastUpdate = 342349218312312

Where you passing the page number and the timestamp for your local db.

您传递本地数据库的页码和时间戳的位置。

Your Json response with number of page and you custom data.

您的Json响应与页数和您的自定义数据。