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
syn2083
Newbie


Joined: 05 Apr 2014
Posts: 5

PostPosted: Mon Apr 07, 2014 8:54 pm   

Need help with a script - MySQL & CMud Pro - EQ Database attempt - INSERT
 
Hi Folks,

I have been combing the threads and the internet over the last few days trying to set up an identify script to pull data into a MySQL database.

I seem to be having quite a bit of difficulty though.

To begin:

Here is a sample output of an object so you can understand what I am dealing with:


Your eyes glow bright yellow for a moment.
You examine '+'Arch-Leggings'+' carefully.
Object 'archeq Leggings' is type armor, worn on legs, extra flags loyal no-quest.
Variable myRecord removed.
This object was created by Syndicate, and is owned by Syndicate.
This item has been upgraded with 0 rival points.
This item is forged with adamantite.
This item has a fragile sword talisman hanging from it.
This item is resistant to offensive spells.
Armor class is 25.
This unholy relic protects the wearer from good.
You must have 10 EQ points to wear this item.
Affects hp by 1750. (Base)
Affects mana by 1750. (Base)
Affects moves by 1750. (Base)
Affects psyche by 20. (Base)
Affects armor class by -250. (Base)
Affects hit roll by 300. (Base)
Affects damage roll by 330. (Base)
Affects move regeneration by 5. (Crafting)
Affects hp multiplier by 0.06%. (Santatium)
Affects damage roll by 12. (Santatium)
Affects damage roll by 12. (Adamantite)
Affects hit roll by 12. (Adamantite)

So my first 'step' or thought was to set up a 'master' variable if you will to contain all of the portions of the identify

#CLASS {identify}
#VAR myRecord %Null

I can add in parts to the variable fine (The UNV line was because the variables were just cascading into eachother if I didnt pre-clear and so thing slike HP would be additive per identify)

Such as

#CLASS {identify}
#VAR myRecord %Null
mydb = %sqldb("Test", "dskies_items", "mysql", "127.0.0.1", 3306)
mydb.LoginPrompt = 1

#TRIGGER {Object '(*)' is type (%x), worn on (*), extra flags (*).$} {
#UNV myRecord
#VAR myRecord object %1
#VAR myRecord type %2
#VAR myRecord wearloc %3
#VAR myRecord extraflags %4
}
#TRIGGER {Armor class is (*).$} {#VAR myRecord AC %1}

If I look at the variable myRecord in CMud i see the values as expected, works great.

Now as far as the insert goes, I have no idea how to just pull the portion of the variable I need for the specific field. I tried a lot of things which didnt work.

My first attempt was something like (edited for brevity):

#CALL @mydb.Open
#CALL @mydb.Execute(%concat("INSERT INTO armor (object, type, wearloc, extraflags, ac) VALUES ('", @myRecord.object, "', '", @myRecord.type, "', '", @myRecord.wearloc, "', '", @myRecord.extraflags, "', '", @myRecord.AC, "')"))
#CALL @mydb.Close
#CLASS 0

This just tries to throw the entire variable into each field of the record though..

I am sure its something easy and newbish but I just cant figure it out, any help would be immensely appreciated. Crying or Very sad
Reply with quote
MattLofton
GURU


Joined: 23 Dec 2000
Posts: 4834
Location: USA

PostPosted: Tue Apr 08, 2014 12:24 am   
 
1)avoid using the #UN commands like #UNVAR. The creation/destruction model is very inefficient in CMud, so it's better to create the variable once and initialize it to some starting value when you're done with it.

2)the normal way to refer to a specific piece of data in a datarecord variable like you have is via @var.key syntax. This isn't supported in all situations, so in those cases where it doesn't work you need to use the longer equivalent of %db(var,key).

3)perhaps try loading @myRecord into a local variable first and then use var.key notation on the local variable instead. Sometimes things work for a local variable where they do not work for a regular variable. Using a local variable will be faster, at any rate.
_________________
EDIT: I didn't like my old signature
Reply with quote
syn2083
Newbie


Joined: 05 Apr 2014
Posts: 5

PostPosted: Tue Apr 08, 2014 3:21 pm   
 
I apologize if I am being dense, or slow, this is unfamiliar and a new baby make the mind muddled.

Be that as it may..

I have tried your suggestions (to my knowledge) and thus far have not been successful.

I think I am either doing something fundamentally wrong, or, something else is wrong i am unable to see.

Even hard coding now I can hardly get it connected.

MySQL workbench now usually just shows the login from cmud with a SLEEP command and I cant seem to get it to add a thing at all, so Im not sure if your suggestions would be working, or are or this is just totally broken or what..

Hard coded, i fire the trigger and it just doesnt show up in the DB, echoes fine on my screen, but nothing in the DB.

So..

Here is a sample of the code that I have placed into a Class folder in the GUI (identify)

Code:
#CLASS identify
mydb3 = %sqldb(myavdb,dskies_items,mysql,127.0.0.1,3306)
mydb3.LoginPrompt = 1
#TRIGGER {Object '(*)' is type (%x), worn on (*), extra flags (*).} {
#CALL @mydb3.Open()
#CALL @mydb3.Execute("INSERT INTO armor (object, type, wearloc, extraflags) VALUES ('", sleeves, "', '", armor, "', '", arms, "', '", none, "')")
#ECHO {Made it down to the insert}
} identify
#CLASS 0


or, trying to just pass the direct result to insert:

Code:
#CLASS identify
mydb3 = %sqldb(myavdb,dskies_items,mysql,127.0.0.1,3306)
mydb3.LoginPrompt = 1
#TRIGGER {Object '(*)' is type (%x), worn on (*), extra flags (*).} {
#CALL @mydb3.Open()
#CALL @mydb3.Execute("INSERT INTO armor (object, type, wearloc, extraflags) VALUES ('", %1, "', '", %2, "', '", %3, "', '", %4, "')")
#ECHO {Made it down to the add, did it}
} identify
#CLASS 0


Or trying a variable in between (not sure if i should use #VAR or #ADDKEY no noticeable difference, but that may be because it doesnt seem to actually fire the INSERT at all:

Code:
#CLASS identify
#VAR myItem %null identify
mydb3 = %sqldb(myavdb,dskies_items,mysql,127.0.0.1,3306)
mydb3.LoginPrompt = 1
#TRIGGER {Object '(*)' is type (%x), worn on (*), extra flags (*).} {
#ADDKEY  myItem object %1
#ADDKEY  myItem type %2
#ADDKEY  myItem wearloc %3
#ADDKEY  myItem extraflags %4
#CALL @mydb3.Open()
#CALL @mydb3.Execute("INSERT INTO armor (object, type, wearloc, extraflags) VALUES ('", @myItem.object, "', '", @myItem.type, "', '", @myItem.wearloc, "', '", @myItem.extraflags, "')")
#ECHO {Made it down to the add, did it}
} identify
#CLASS 0


IS there anything inherently wrong, or bad here?

I cant for the life of me figure out why it wont insert anything at all now and just seems to sit in a 'sleep state'

If I try to close it, it doesnt seem to help on re-opening it, so I am not sure what to do there..

Again apologies if this is rambling and not terribly coherent..

Thank you again!
Reply with quote
syn2083
Newbie


Joined: 05 Apr 2014
Posts: 5

PostPosted: Tue Apr 08, 2014 4:51 pm   
 
Ok so, I am really silly. adding (%concat before the INSERT fixed it not adding anything..

BUT..

It only adds if the INSERT is within the trigger the Variable Key/Value is set in, if it is outside of that trigger it doesnt add anything for no reason I can tell..

If I put the INSERT call into it's own trigger, (within the same class) and try, nothing hits the DB.. I tried with the DB connection in the trigger, at the class script level, local variable conversion, and it doesnt do anything..

So its bizarre, because with the concat, if I do same trigger I can use @myItem.object for instance, and it hits the DB fine. but removed from the trigger and nada..
Reply with quote
syn2083
Newbie


Joined: 05 Apr 2014
Posts: 5

PostPosted: Tue Apr 08, 2014 7:57 pm   
 
Further work has provided that I can add a record, if I whittle down every option that a specific item does not have in identify to not be sent on Insert.

I tried to set a valid value for each of the fields in general to initialize them so if they are passed it is with a benign value that I can ignore, but MySQL didnt seem to like it for some reason, i think most likely because most of the fields need to be evaluated.

So a piece of gear may have
Affects hp by 25
Affects damage roll by 10
Affects hp by 25

The second (or more) are from questing or whatnot. So I set up an eval to add the successive values into the start. Works fine, normally, but maybe it has a problem with evaluating nothing + nothing, or nothing + (some default, eg 0)

Then it tries to pass this to MySQL as some form of non existent data?

Im not sure if that made any sense, or what i can actually do about it. I am sure there is an answer I just havent found it yet.. Hopefuly someone can help me out there :D
Reply with quote
syn2083
Newbie


Joined: 05 Apr 2014
Posts: 5

PostPosted: Wed Apr 09, 2014 12:37 am   
 
Well, I continue to be ridiculous..

I figured it out, again. With my lack of sleep I was setting a default value of say 0 for a key.value IN the trigger. So if the trigger couldnt fire, because no stat entry, no default value was being set... ridiculous.

I moved my sets to an alias for identify, and it works a charm now.

its probably still completely inelegant, but it works every time. After I clean it up I will post it for anyone interested.
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