Update database version in Lua

2019-04-14 17:14发布

I'm trying to update the database version but, I'm facing a strange problem... Here's my code:

version = 2
local cur_db_version=nil
for data in db:nrows("PRAGMA user_version") do
    cur_db_version=data
    break
end
local db_version=cur_db_version.user_version
print(db_version)
print(version)
if (db_version~=version) then
    create()
    db:exec("PRAGMA user_version="..version)
end

Imagining that the database version is 1, I'm expecting that my script prints this:

1
2

But what it is printing is:

2
2

Why? I don't know but, I do know that if I comment the line db:exec("PRAGMA user_version="..version), then it really prints this:

1
2

So, what's wrong in my code that causes the wrong output? Is the line db:exec("PRAGMA user_version="..version) somehow being executed before the prints? If so, how can I change my code in order that it only calls create() when version changes?

By the way, I'm using Corona SDK but, I don't believe that the problem is related to it right?

1条回答
▲ chillily
2楼-- · 2019-04-14 17:36

Ok, I found the issue... The above code is correct and the problem was with a script that I have on sublime to relaunch the simulator when I save the file and what was happenning is that somehow, the script was relaunching the simulator 2 consecutive times and due to the many prints that I have, I didn't saw the correct output above on the console...

I will keep this question here just because there could be someone else that want to know how to update the database version on corona/lua.

查看更多
登录 后发表回答