Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Example use of SQL

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Dark Byte
Site Admin
Reputation: 340

Joined: 09 May 2003
Posts: 19908
Location: The netherlands

PostPosted: Wed Aug 02, 2017 5:01 pm    Post subject: Example use of SQL Reply with quote

here is an example of using sqlite3. Those that know how to use databases may understand how to use this.

Code:

c=createSQLite3Connection()
c.DatabaseName='e:\\something.sqlite3'
c.Connected=true

t=createSQLTransaction()
t.SQLConnection=c
t.Active=true

tn=c.getTableNames()
if #tn==0 then
  print("empty")

  c.ExecuteDirect([[
    CREATE TABLE something (
    'id' INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    'processname' char(256) NOT NULL,
    'rating' INTEGER);
  ]])
  tn=c.getTableNames()
  if #tn==0 then error('no table created') end

end

print("tables:")
local hassomething=false
local i
for i=1,#tn do
  print(tn[i])
  if tn[i]=='something' then hassomething=true end
end

if hassomething then
  pn=process
  if (pn==nil) or (p=='nil') then pn='no process'  end

  --check if pn is in the table, if so, increment rating, else add it
  q=createSQLQuery()
  q.SQLConnection=c
  q.SQLTransaction=t

  q.SQL.Text=string.format([[ Select * from something where processname='%s' ]],pn) --not secure, I know, just an example
  q.Active=true

  if q.RecordCount>0 then
    print("In the list")
    fields=q.Fields
    print("fieldcount="..fields.Count)

    local id=q.FieldByName('id').asInteger
    print("id="..id)

    local rating=q.FieldByName('rating')
    print("rating="..rating.asInteger)

    q.Active=false
    q.StatementType='stUpdate'
    q.SQL.Text=string.format([[update something set rating=rating+1 where id=%d]], id)
    q.ExecSQL()

  else
    print("Not yet in the list")
    c.ExecuteDirect(string.format([[insert into something(processname,rating) values('%s',1)]],pn))
  end

  q.Active=false

  q.StatementType='stSelect'
  q.SQL.Text=[[select * from something]]
  q.Active=true

  count=1
  print(string.format("RecordCount=%d", q.RecordCount))
  while not q.EOF do
    print("line "..count)
    local i
    for i=0, q.Fields.Count-1 do
      print(string.format("  %s = %s", q.Fields[i].FieldName, q.Fields[i].Value))
    end

    q.next()
    count=count+1
  end

  q.Active=false
  q.destroy()

else
  error("something doesn't exist")
end

t.Commit()
t.Active=false
c.Connected=false

t.destroy()
c.destroy()   

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites