Trouble inserting data into an sqlite3 database
#10
(11-22-2015, 09:31 AM)DrMasik Wrote: Any questions?

Sorry, I think you may have missed a part of the conversation. I managed to insert data, using prepared statements.
The problem I have now is using a SELECT query: if the result contains data from the database it runs fine, if not, it errors out instead.
Like I said, it breaks on stmt:get_values().

@xoft, I just noticed I completely missed your response, sorry for that. I'm indeed trying to write a wrapper around the database, so I can just run "ExecuteStatement" with my query and an array of parameters. It works fine for now, except when using SELECT with no result.

My current complete code:
function ExecuteStatement(sql, parameters)
	local db = sqlite3.open(PLUGIN:GetLocalFolder() .. "/database.sqlite3")
	local stmt = db:prepare(sql)

	if not (parameters == nil) then
		for key, value in pairs(parameters) do
			stmt:bind(key, value)
		end
	end

	stmt:step()

	if (sql:match("SELECT")) then
		result = stmt:get_values()
	elseif (sql:match("INSERT")) then
		result = stmt:last_insert_rowid()
	end

	stmt:finalize()

	if not (result == nil) then
		return result
	end

	db:close()
end

last_insert_rowid() breaks too but I have to look a little bit more into that before I ask any questions about it Wink
Reply
Thanks given by:


Messages In This Thread
RE: Trouble inserting data into an sqlite3 database - by PureTryOut - 11-23-2015, 01:34 AM



Users browsing this thread: 4 Guest(s)