I want shared talbe in different world.
How can I do it?
How can I do it?
This forum is a read-only archive of the Gammon Software forum (2000–2026). No new posts can be made. Search the archive.
Posted by Narci on Sun 27 Jul 2014 10:54 AM — 14 posts, 46,533 views.
require "tprint"
DatabaseOpen ("db", GetInfo (66) .. "mytestdb.sqlite", 6)
rc = DatabaseExec ("db", [[
DROP TABLE IF EXISTS weapons;
CREATE TABLE weapons(
weapon_id INTEGER NOT NULL PRIMARY KEY autoincrement,
name TEXT NOT NULL,
damage INT default 10,
weight REAL
);
]])
-- put some data into the database
DatabaseExec ("db",
[[
INSERT INTO weapons (name, damage) VALUES ('sword', 42);
INSERT INTO weapons (name, damage) VALUES ('mace', 55);
INSERT INTO weapons (name, damage) VALUES ('staff', 35);
]])
-- prepare a query
DatabasePrepare ("db", "SELECT * from weapons ORDER BY name")
-- find the column names
names = DatabaseColumnNames ("db")
tprint (names)
-- execute to get the first row
rc = DatabaseStep ("db") -- read first row
-- now loop, displaying each row, and getting the next one
while rc == 100 do
print ("")
values = DatabaseColumnValues ("db")
tprint (values)
rc = DatabaseStep ("db") -- read next row
end -- while loop
-- finished with the statement
DatabaseFinalize ("db")
DatabaseClose ("db") -- close it
local aa='staff'
DatabaseExec ("db",
[[
INSERT INTO weapons (name, damage) VALUES ('sword', 42);
INSERT INTO weapons (name, damage) VALUES ('mace', 55);
INSERT INTO weapons (name, damage) VALUES (aa, 35);
]])
DatabaseExec ("db",
[[
INSERT INTO weapons (name, damage) VALUES ('sword', 42);
INSERT INTO weapons (name, damage) VALUES ('mace', 55);
INSERT INTO weapons (name, damage) VALUES ("blade", 33);
]])
aa="staff"
DatabaseExec ("db", "UPDATE weapons SET name=aa WHERE damage=55;")
weaponType = "staff"
damage = 42
DatabaseExec ("db", string.format (
"INSERT INTO weapons (name, damage) VALUES ('%s', %i);",
weaponType, damage))
-- Quote the argument, replacing single quotes with two lots of single quotes.
-- If nil supplied, return NULL (not quoted).
function fixsql (s)
if s then
return "'" .. (string.gsub (s, "'", "''")) .. "'"
else
return "NULL"
end -- if
end -- fixsql
weaponType = "Nick's staff"
damage = 42
DatabaseExec ("db", string.format (
"INSERT INTO weapons (name, damage) VALUES (%s, %i);",
fixsql (weaponType), damage))
local MUSHclient_Database_Errors = {
[-1] = "Database id not found",
[-2] = "Database not open",
[-3] = "Already have prepared statement",
[-4] = "Do not have prepared statement",
[-5] = "Do not have a valid row",
[-6] = "Database already exists under a different disk name",
[-7] = "Column number out of range",
} -- end of MUSHclient_Database_Errors
-- check for errors on a DatabaseXXXXX call
function dbcheck (code)
if code == sqlite3.OK or -- no error
code == sqlite3.ROW or -- completed OK with another row of data
code == sqlite3.DONE then -- completed OK, no more rows
return code
end -- if code OK
-- DatabaseError won't return the negative errors
local err = MUSHclient_Database_Errors [code] or DatabaseError("db")
DatabaseExec ("db", "ROLLBACK") -- rollback any transaction to unlock the database
error (err, 2) -- show error in caller's context
end -- dbcheck
dbcheck (DatabaseExec ("db", string.format (
"INSERT INTO weapons (name, damage) VALUES (%s, %i);",
fixsql (weaponType), damage)))
dbcheck (DatabaseExec ("db", "INSERT INTO weapons (name, damage) VALUES (aa, 35);"))
Run-time error
World: smaug2
Immediate execution
[string "Immediate"]:66: no such column: aa
stack traceback:
[C]: in function 'error'
[string "Immediate"]:26: in function 'dbcheck'
[string "Immediate"]:66: in main chunk
DatabaseExec ("db",
[[
INSERT INTO weapons (name, damage) VALUES ('sword', 42);
INSERT INTO weapons (name, damage) VALUES ('mace', 55);
INSERT INTO weapons (name, damage) VALUES ("blade", 33);
]])
aa="staff"
DatabaseExec ("db", "UPDATE weapons SET name=aa WHERE damage=55;")
aa="staff"
DatabaseExec ("db", string.format ("UPDATE weapons SET name=%s WHERE damage=55;", fixsql (aa)))
You have completed the daily quest.
<triggers>
<trigger
enabled="y"
match="You have completed the daily quest."
send_to="12"
sequence="100"
>
<send>
name = "my_second_world"
otherworld = GetWorld (name)
if otherworld == nil then
Note ("World " .. name .. " is not open")
return
end
Execute (otherworld, "NOTIFICATION: %0")
</send>
</trigger>
</triggers>
<aliases>
<alias
match="NOTIFICATION: *"
enabled="y"
send_to="12"
sequence="100"
>
<send>Note ("You received message: %1")</send>
</alias>
</aliases>
<triggers>
<trigger
enabled="y"
match="You have completed the daily quest."
send_to="12"
sequence="100"
>
<send>
for k, v in pairs (GetWorldIdList ()) do
if v ~= GetWorldID() then -- don't send to ourself
GetWorldById (v):Execute ("NOTIFICATION: %0")
end -- if
end
</send>
</trigger>
</triggers>