|
hogarius Adept
Joined: 29 Jan 2003 Posts: 221 Location: islands.genesismuds.org
|
Posted: Sat Sep 03, 2011 9:40 pm
[3.34] %sql Insert method |
Can anyone give an example and an explanation on how to use the Insert method of the %sql function?
|
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Sun Sep 04, 2011 9:44 pm |
I think I covered this in another post. To use SQL commands that do not return any query results, you need to use the Execute method of the actual database connection and not the %sql function. Use %sqldb to open a database connection and store the object in a variable, then use the Execute method. Something like this:
Code: |
db = %sqldb("MyDb.db")
#call @db.Execute("INSERT INTO table (column1, column2, ...) VALUES (value1, value2...)") |
|
|
|
|
hogarius Adept
Joined: 29 Jan 2003 Posts: 221 Location: islands.genesismuds.org
|
Posted: Sun Sep 04, 2011 10:06 pm |
So, there's no easy way to dump all of a CMUD database variable directly into a SQLite record with the same fields?
As an example, if I have a CMUD variable @Crook with 18 Key=Value pairs, and a SQLite table CrooksTable with 18 corresponding fields, there's no function like %sqldb(CrooksTable.RecAdd)=@Crook ? |
|
|
|
Rahab Wizard
Joined: 22 Mar 2007 Posts: 2320
|
Posted: Fri Sep 09, 2011 12:52 pm |
No, there is no easy way. However, it would be possible to write a function which would take a db variable and generate an INSERT statement. Actually, that would not be too difficult. I don't have Cmud on this computer, but off the top of my head it might look something like this:
Code: |
#FUNCTION dumpvar {
$columns = %expandlist(%dbkeys(@Crook), ", ")
$values = %expandlist(%dbvalues(@Crook), ", ")
#CALL @db.Execute(%concat("INSERT INTO table (", $columns, ") VALUE (", $values, ")"))
}
|
Then you can just do:
|
|
|
|
hogarius Adept
Joined: 29 Jan 2003 Posts: 221 Location: islands.genesismuds.org
|
Posted: Fri Sep 09, 2011 10:37 pm |
I did some playing with the %sqldb and %sql functions, and the following works.
Code: |
#sqldb Crooks.db
Crec = %sql(Crooks, "SELECT * FROM Crooks")
#call @Crec.Edit
#call @Crec.Insert
#call @Crec.Post
#loopdb @Crook {#exec "Crec.Item(%key) = %val"}
#call @Crec.Edit
#call @Crec.Post
|
|
|
|
|
|
|