Difference between revisions of "Sqlite3"

From GiderosMobile
m
 
(14 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
'''Supported platforms:''' android, ios, mac, pc<br/>
+
<!-- GIDEROSOBJ:sqlite3 -->
 +
'''Supported platforms:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]][[File:Platform html5.png]][[File:Platform winrt.png]][[File:Platform win32.png]]<br/>
 
'''Available since:''' Gideros 2012.2.1<br/>
 
'''Available since:''' Gideros 2012.2.1<br/>
 +
 
=== Description ===
 
=== Description ===
<translate><br />
+
Gideros runtime supports public domain SQLite3 database engine for iOS, Android and Desktop platforms.
Gideros runtime supports public domain SQLite3 database engine for iOS, Android and Desktop platforms. <br />
+
<syntaxhighlight lang="lua">
For more information and detailed documentation, please visit [http://lua.sqlite.org](http://lua.sqlite.org).<br />
+
require "lsqlite3"
<br />
+
</syntaxhighlight>
&lt;h3&gt;SQLite3 on Android platforms&lt;/h3&gt;<br />
+
 
Currently, LuaSQLite3 plugin cannot open databases stored in APK files. Therefore, database file should be<br />
+
'''For more information and detailed documentation, please visit [http://lua.sqlite.org](http://lua.sqlite.org)'''
copied from resource directory to documents directory.<br />
+
 
<br />
+
==== SQLite3 on Android platforms ====
To copy a file, this function can be used:<br />
+
Currently, LuaSQLite3 plugin cannot open databases stored in APK files. Therefore, database file should be copied from resource directory to documents directory.
<br />
+
 
&lt;pre&gt;&lt;code&gt;<br />
+
To copy a file, this function can be used:
local function copy(src, dst)<br />
+
<syntaxhighlight lang="lua">
local srcf = io.open(src, &quot;rb&quot;)<br />
+
local function copy(src, dst)
local dstf = io.open(dst, &quot;wb&quot;)<br />
+
local srcf = io.open(src, "r")
<br />
+
local dstf = io.open(dst, "wb")
local size = 2^13     -- good buffer size (8K)<br />
+
local size = 2^13 -- good buffer size (8K)
while true do<br />
+
while true do
local block = srcf:read(size)<br />
+
local block = srcf:read(size)
if not block then break end<br />
+
if not block then break end
dstf:write(block)<br />
+
dstf:write(block)
end<br />
+
end
<br />
+
srcf:close()
srcf:close()<br />
+
dstf:close()
dstf:close()<br />
+
end
end<br />
+
</syntaxhighlight>
<br />
+
 
&lt;/code&gt;&lt;/pre&gt;<br />
+
Also it is better to check if the database file is previously copied or not. To check if a file can be found on the underlying file system, this function can be used:
Also it&#039;s better to check if the database file is previously copied or not. To check if a file can be found <br />
+
<syntaxhighlight lang="lua">
on the underlying file system, this function can be used:<br />
+
local function exists(file)
<br />
+
local f = io.open(file, "rb")
&lt;pre&gt;&lt;code&gt;<br />
+
if f == nil then return false end
local function exists(file)<br />
+
f:close()
local f = io.open(file, &quot;rb&quot;)<br />
+
return true
if f == nil then<br />
+
end
return false<br />
+
</syntaxhighlight>
end<br />
+
 
f:close()<br />
+
By using these two function, you can copy the database file from resource to documents directory before using it:
return true<br />
+
<syntaxhighlight lang="lua">
end<br />
+
if not exists("|D|database.db") then
<br />
+
copy("database.db", "|D|database.db")
&lt;/code&gt;&lt;/pre&gt;<br />
+
end
By using these two function, you can copy the database file from resource to documents directory before using it:<br />
+
</syntaxhighlight>
<br />
+
 
&lt;pre&gt;&lt;code&gt;<br />
+
==== SQLite3 on on WinRT need to add an extension to Visual Studio ====
if not exists(&quot;|D|database.db&quot;) then<br />
+
Gideros now supports sqlite and to make this work on Windows Phone, it is necessary to add an extension to Visual Studio. Even if you don't use sqlite, you will need to add this extension or Windows Phone projects exported by Gideros will not compile. It's easy to do as explained next. Of course, you only need to add the extension once.
copy(&quot;database.db&quot;, &quot;|D|database.db&quot;)<br />
+
 
end<br />
+
Open Visual Studio and go to ''TOOLS > Extensions and Updates''. Select the ''Online'' tab on the left and type sqlite in the search bar at the top right. Select ''SQLite'' for Windows 8.1 or/and Windows Phone 8.1 and select install. You will need to restart VS for the extension to take effect.
<br />
+
 
&lt;/code&gt;&lt;/pre&gt;<br />
+
=== Examples ===
 +
'''Simple example'''
 +
<syntaxhighlight lang="lua">
 +
local sqlite3 = require "lsqlite3"
 +
print(sqlite3.version())
 +
local db = sqlite3.open("db/mydb2.gdb") -- path to your db
 +
 
 +
db:exec[=[
 +
CREATE TABLE numbers(num1,num2,text);
 +
]=]
 +
 
 +
db:exec[=[
 +
INSERT INTO numbers VALUES(1,101,"a");
 +
INSERT INTO numbers VALUES(2,202,"b");
 +
INSERT INTO numbers VALUES(3,303,"c");
 +
]=]
 +
for num1,num2,text in db:urows('SELECT * FROM numbers') do
 +
print(num1,num2,text)
 +
end
 +
 
 +
db:close()
 +
</syntaxhighlight>
 +
 
 +
'''Complete example with SQLite3 binding by Atilim'''
 +
<syntaxhighlight lang="lua">
 +
require "lsqlite3"
 +
 +
-- open the database
 +
local db = sqlite3.open("|D|db.sqlite3")
 +
 +
-- check if 'numbers' table exists
 +
local exists = false
 +
db:exec("SELECT name FROM sqlite_master WHERE type='table' AND name='numbers'", function() exists = true end)
 +
 +
-- if not, create it and put some values
 +
if not exists then
 +
local sql = [[
 +
CREATE TABLE numbers(num,str);
 +
INSERT INTO numbers VALUES(1,'ABC');
 +
INSERT INTO numbers VALUES(2,'DEF');
 +
INSERT INTO numbers VALUES(3,'UVW');
 +
INSERT INTO numbers VALUES(4,'XYZ');
 +
]]
 +
 +
db:exec(sql)
 +
end
 +
 +
-- create a prepared statement
 +
local stmt = db:prepare("SELECT * FROM numbers WHERE num=? AND str=?")
 +
stmt:bind(1, 1)  -- bind 1st value as 1
 +
stmt:bind(2, "ABC")  -- bind 2nd value as "ABC"
 +
 +
-- execute and print the result
 +
for num,str in stmt:urows() do
 +
print(num, str)
 +
end
 +
 +
--finalize statement to free resources
 +
stmt:finalize()
  
&lt;h3&gt;SQLite3 on on WinRT need to add an extension to Visual Studio&lt;/h3&gt;
+
-- close the database
 +
db:close()
 +
</syntaxhighlight>
  
Gideros now supports sqlite and to make this work on Windows Phone, it is necessary to add an extension to Visual Studio. Even if you don't use sqlite, you will need to add this extension or Windows Phone projects exported by Gideros will not compile. It's easy to do as explained next. Of course, you only need to add the extension once.
 
&lt;br/&gt;
 
Open Visual Studio and go to [[TOOLS > Extensions and Updates]]. Select the [[Online]] tab on the left and type sqlite in the search bar at the top right. Select [[SQLite]] for Windows 8.1 or/and Windows Phone 8.1 and select install. You will need to restart VS for the extension to take effect.</translate>
 
 
{|-
 
{|-
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
 +
 
=== Methods ===
 
=== Methods ===
 +
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
 
=== Events ===
 
=== Events ===
 
=== Constants ===
 
=== Constants ===
 
|}
 
|}
 +
 +
{{GIDEROS IMPORTANT LINKS}}

Latest revision as of 12:52, 15 February 2024

Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.pngPlatform html5.pngPlatform winrt.pngPlatform win32.png
Available since: Gideros 2012.2.1

Description

Gideros runtime supports public domain SQLite3 database engine for iOS, Android and Desktop platforms.

require "lsqlite3"
For more information and detailed documentation, please visit [1](http://lua.sqlite.org)

SQLite3 on Android platforms

Currently, LuaSQLite3 plugin cannot open databases stored in APK files. Therefore, database file should be copied from resource directory to documents directory.

To copy a file, this function can be used:

local function copy(src, dst)
	local srcf = io.open(src, "r")
	local dstf = io.open(dst, "wb")
	local size = 2^13 -- good buffer size (8K)
	while true do
		local block = srcf:read(size)
		if not block then break end
		dstf:write(block)
	end
	srcf:close()
	dstf:close()
end

Also it is better to check if the database file is previously copied or not. To check if a file can be found on the underlying file system, this function can be used:

local function exists(file)
	local f = io.open(file, "rb")
	if f == nil then return false end
	f:close()
	return true
end

By using these two function, you can copy the database file from resource to documents directory before using it:

if not exists("|D|database.db") then
	copy("database.db", "|D|database.db")
end

SQLite3 on on WinRT need to add an extension to Visual Studio

Gideros now supports sqlite and to make this work on Windows Phone, it is necessary to add an extension to Visual Studio. Even if you don't use sqlite, you will need to add this extension or Windows Phone projects exported by Gideros will not compile. It's easy to do as explained next. Of course, you only need to add the extension once.

Open Visual Studio and go to TOOLS > Extensions and Updates. Select the Online tab on the left and type sqlite in the search bar at the top right. Select SQLite for Windows 8.1 or/and Windows Phone 8.1 and select install. You will need to restart VS for the extension to take effect.

Examples

Simple example

local sqlite3 = require "lsqlite3"
print(sqlite3.version())
local db = sqlite3.open("db/mydb2.gdb") -- path to your db

db:exec[=[
	CREATE TABLE numbers(num1,num2,text);
]=]

db:exec[=[
	INSERT INTO numbers VALUES(1,101,"a");
	INSERT INTO numbers VALUES(2,202,"b");
	INSERT INTO numbers VALUES(3,303,"c");
]=]
for num1,num2,text in db:urows('SELECT * FROM numbers') do
	print(num1,num2,text)
end

db:close()

Complete example with SQLite3 binding by Atilim

require "lsqlite3"
 
-- open the database
local db = sqlite3.open("|D|db.sqlite3")
 
-- check if 'numbers' table exists
local exists = false
db:exec("SELECT name FROM sqlite_master WHERE type='table' AND name='numbers'", function() exists = true end)
 
-- if not, create it and put some values
if not exists then
	local sql = [[
		CREATE TABLE numbers(num,str);
		INSERT INTO numbers VALUES(1,'ABC');
		INSERT INTO numbers VALUES(2,'DEF');
		INSERT INTO numbers VALUES(3,'UVW');
		INSERT INTO numbers VALUES(4,'XYZ');
	]]
 
	db:exec(sql)
end
 
-- create a prepared statement
local stmt = db:prepare("SELECT * FROM numbers WHERE num=? AND str=?")
stmt:bind(1, 1)  -- bind 1st value as 1
stmt:bind(2, "ABC")  -- bind 2nd value as "ABC"
 
-- execute and print the result
for num,str in stmt:urows() do
	print(num, str)
end
 
--finalize statement to free resources
stmt:finalize()

-- close the database
db:close()

Methods

Events

Constants