Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
hogarius
Adept


Joined: 29 Jan 2003
Posts: 216
Location: islands.genesismuds.org

PostPosted: 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?
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23377
Location: Colorado, USA

PostPosted: 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...)")
Reply with quote
hogarius
Adept


Joined: 29 Jan 2003
Posts: 216
Location: islands.genesismuds.org

PostPosted: 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 ?
Reply with quote
Rahab
Wizard


Joined: 22 Mar 2007
Posts: 2307

PostPosted: 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:
Code:
#CALL @dumpvar()
Reply with quote
hogarius
Adept


Joined: 29 Jan 2003
Posts: 216
Location: islands.genesismuds.org

PostPosted: 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

Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
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

© 2009 Zugg Software. Hosted by Wolfpaw.net