|
Iliev Newbie
Joined: 20 Jun 2011 Posts: 8
|
Posted: Tue Jun 21, 2011 1:22 pm
adding entries to sqlite db |
#call %sql(INSERT INTO eq (asd1) values(2))
this is what I type, there's a db that I created with #sqldb eqsql2, eq is a table and asd1 is a field (column) which I created in some sqlite viewer. So how can I add info, it doesn't seem to work and the help file examples are too few. |
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Tue Jun 21, 2011 10:48 pm |
Make sure to put " quotes around the argument to %sql. That's extra important when using stuff that contains it's own special characters like ()
So try this:
Code: |
#call %sql("INSERT INTO eq (asd1) values (2)") |
and it should work. |
|
|
|
Iliev Newbie
Joined: 20 Jun 2011 Posts: 8
|
Posted: Wed Jun 22, 2011 5:32 pm |
I type
#sqldb test1
It creates a db called test1 in d:\cmud sessions\sessionname
Then if I type:
#call %sql(test1, “insert into eq (asd1) values (asd2)”)
The error message is: SQL logic error or missing database.
If I type:
#call %sql("insert into eq (asd1) values (asd2)")
It doesn’t output anything.
There is no table eq or field asd1 in it, however when I add those with external viewer, nothing gets added either.
Also, the help file says to use POST method, but I can't find the syntax :( |
|
|
|
Daern Sorcerer
Joined: 15 Apr 2011 Posts: 809
|
Posted: Wed Jun 22, 2011 11:39 pm |
I have to agree, the documentation for all the sql commands/functions are rather terrible. I believe you can't use insert queries like that, Iliev, only select queries. I'm not sure though, since the documention doesn't say. I think you need to use the %sql function's insert method to insert records into your database, but I'm not positive, since the documentation says nothing about it. Unfortunately, I couldn't figure out how to use that method, since it has absolutely zero documentation, beyond a description. Are we supposed to figure out how to use these (relatively) new features by trial and error?
BTW, if someone else had more luck with their trial and error than I did, and actually knows how to use more than three of the 21 properties/methods of the %sql function, please do post, with examples preferably (I say more than three because there is only one example given, and it only uses three of the properties/methods. In fact, all four sql-related commands/functions have that same, single, very basic example in the documentation...). |
|
|
|
Anaristos Sorcerer
Joined: 17 Jul 2007 Posts: 821 Location: California
|
Posted: Thu Jun 23, 2011 1:26 am |
Quote: |
“insert into eq (asd1) values (asd2)”)
|
I am going to assume that asd1 is a column name, so that makes asd2 a literal string (since nothing else makes sense). In that case:
Code: |
#CALL %sql(test1,"INSERT INTO eq (asd1) VALUES ('asd2')")
|
is the correct syntax.
I don't play with the cmud sql functions much so I am not going to claim I know how they work, but unless I misread the documentation they don't create tables for you, so before you do the insert you have to create the table.
Code: |
$sql = "CREATE TABLE [eq] ([asd1] TEXT)" // you can declare the column whatever data type you want.
#CALL %sql(test1,$sql)
|
|
|
_________________ Sic itur ad astra. |
|
|
|
Daern Sorcerer
Joined: 15 Apr 2011 Posts: 809
|
Posted: Thu Jun 23, 2011 2:06 am |
Well, what do you know, it worked perfectly! Why can't the official documentation be this clear?
|
|
|
|
charneus Wizard
Joined: 19 Jun 2005 Posts: 1876 Location: California
|
Posted: Thu Jun 23, 2011 2:12 am |
Edit: Ninja'd... that's what happens when you start a post and forget about it an hour or two later.
The key step you seem to be missing is creating the table 'eq'. The bad SQL logic comes from you trying to insert info into a non-existent table. I created the table and the INSERT worked just fine for me (meaning it popped up no errors).
As for the POST thing, I've no clue what it's referring to, either. |
|
|
|
Daern Sorcerer
Joined: 15 Apr 2011 Posts: 809
|
Posted: Thu Jun 23, 2011 2:25 am |
It's referring to the %sql().Post() method. As far as I can tell, it writes data back to the database after changes have been made with other methods, like insert. Unfortunately, since the documentation is missing so much crucial information, like the syntax to use these methods, I never did figure out how to use them properly. But it turns out you can use insert SQL queries, so using pure SQL is probably the easiest way to manipulate your databases, rather than the cmud %sql function's methods, since SQL is well-documented on the internet.
|
|
|
|
Anaristos Sorcerer
Joined: 17 Jul 2007 Posts: 821 Location: California
|
Posted: Thu Jun 23, 2011 2:30 am |
I think you can ignore that suggestion of the Post method for the time being. The code assumed that you were doing multiple edits to the table therefore the (erroneous) assumption was that a BEGIN TRANSACTION was hanging about. THE POST/COMMIT would have caused the changes to the table to become actualized. However, since all came about due to the fact that the table didn't exist to begin with, I doubt it is applicable.
|
|
_________________ Sic itur ad astra. |
|
|
|
Daern Sorcerer
Joined: 15 Apr 2011 Posts: 809
|
Posted: Thu Jun 23, 2011 2:33 am |
Why do you say the table didn't exist? It did, the only problem with Iliev's code was some missing quotes around his string.
|
|
|
|
charneus Wizard
Joined: 19 Jun 2005 Posts: 1876 Location: California
|
Posted: Thu Jun 23, 2011 2:40 am |
Edit: Didn't see the table creation with a sql viewer. Not sure then why I can do it without quotes. :p
|
|
|
|
Iliev Newbie
Joined: 20 Jun 2011 Posts: 8
|
Posted: Thu Jun 23, 2011 9:49 am |
I'm not sure what's going on, it works now. Including my own example that I posted last night, and which I tried like 30 times and I'm sure it wasn't working.
The field is a number and my example value of asd should be wrong, but it's not what caused the error.
When no such table called eq, there's a message saying there isn't a table.
When I try adding asd as a value, and without ' ' as Anaristos said, there's a different error message (not the logic / missing db one)
Also, when there's no such field, there's a corresponding error as well.
So I'm not sure what the problem was. :P |
|
|
|
aslanthekat Novice
Joined: 17 Sep 2007 Posts: 44 Location: Missouri
|
Posted: Thu Jun 23, 2011 1:15 pm |
I've tried to use this syntax but I keep getting an error. Looking at everyones examples I don't see what I am doing differently. Maybe someone will notice something.
Code: |
#SQLDB myavdb databasename mysql hostname 3306 user password
#call %sql(myavdb, "INSERT INTO gear VALUES ('hit gear','something','something else','wield','5hr 5dr')")
#SQLCLOSE myavdb |
When I use that syntax I get an error that says
Quote: |
Can not open a ResultSet |
Even though it gives me an error it does post the data to my database.
Thanks |
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Thu Jun 23, 2011 7:33 pm |
Well, I'm not about to try and document all of SQL here. But the INSERT command has the format:
Code: |
INSERT INTO tablename (fieldname1,fieldname2,...) VALUES (value1,value2,...) |
So your above example is missing the list of field names.
Methods like Post only work if you use %sql to return a data set (from a SELECT) that you want to manipulate using the field properties and methods. If you just use %sql to execute direct SQL statements, then you don't need any of the methods like Post.
Stuff like properly quoting string values in your SQL statement are just part of learning the correct SQL syntax and not something I'm going to document here. The %sql function in CMUD assumes you already have the SQL knowledge that you need.
I also can't do much about the error messages. Those come from the specific database driver DLL code (SQLite, MySQL, etc) or from the ZeosLib database library and are not something I have any control over within CMUD. |
|
|
|
aslanthekat Novice
Joined: 17 Sep 2007 Posts: 44 Location: Missouri
|
Posted: Thu Jun 23, 2011 9:09 pm |
I went ahead and tried your way and I still get the same error.
Code: |
#call %sql(myavdb, "INSERT INTO gear (type, name, shortname, wearlocation, stats) VALUES ('tank gear','something','something else','wield','5hr 5dr')") |
|
|
|
|
Daern Sorcerer
Joined: 15 Apr 2011 Posts: 809
|
Posted: Fri Jun 24, 2011 12:49 am |
I'm not asking you to document SQL, I actually stated above that it's already well-documented on the internet. I'm just asking you to document cmud's sql commands/functions.
|
|
|
|
Zugg MASTER
Joined: 25 Sep 2000 Posts: 23379 Location: Colorado, USA
|
Posted: Fri Jun 24, 2011 8:32 pm |
Documentation already exists: #SQLDB %sql %sqldb
Also, anybody who has a forum account can add a comment to any documentation article to add examples or additional help. So if you see something that needs to be clarified, add it and it will eventually be incorporated into the main doc file.
But back to the original problem. Looking at the ZeosLib code, it looks like %sql is made for queries that return an actual dataset (like SELECT queries). For queries that do not return a dataset, open your database using the %sqldb function so that you have the connection object, then use the Execute property. For example:
Code: |
mydb = %sqldb(myavdb,databasename,mysql,hostname,3306,user,password)
#call @mydb.Open()
#call @mydb.Execute("INSERT INTO gear (type, name, shortname, wearlocation, stats) VALUES ('tank gear','something','something else','wield','5hr 5dr')") |
See if that helps at all. |
|
|
|
Daern Sorcerer
Joined: 15 Apr 2011 Posts: 809
|
Posted: Sat Jun 25, 2011 2:51 am |
Sure, the documentation exists, but it's pretty much useless in its current state. The documentation for the %sql function lists a bunch of methods, but no syntax for those methods... as I stated above, are we just supposed to guess the syntax?
|
|
|
|
aslanthekat Novice
Joined: 17 Sep 2007 Posts: 44 Location: Missouri
|
Posted: Mon Jun 27, 2011 12:59 pm |
That worked perfectly. Thank you so much.
|
|
|
|
aslanthekat Novice
Joined: 17 Sep 2007 Posts: 44 Location: Missouri
|
Posted: Fri Jul 08, 2011 4:22 pm |
One more quick thing... I have these two aliases dcquery & dcadd respectively
Code: |
conn
row = %sql(myavdb, %concat("SELECT * FROM dclocation where race= '",@dcraceq,"' AND level= '",@dclevelq,"'"))
#WHILE (!@row.Eof()) {dcloc = @row.Item("location")
#sa Locations: @row.Item("location")
#CALL @row.Next}
#SQLCLOSE myavdb} |
and
Code: |
mydb = %sqldb(yada yada)
#call @mydb.Open()
#call @mydb.Execute("UPDATE dclocation set location='here' where race='ent' AND level='1'") |
They both work... dcadd will add data to my database, and dcquery will grab it and display it. However, if I run dcquery AFTER dcadd I get the following error
Quote: |
Error parsing command:
Invalide pointer operation
dcquery ent 1 |
|
|
|
|
Daern Sorcerer
Joined: 15 Apr 2011 Posts: 809
|
Posted: Fri Jul 08, 2011 4:57 pm |
You're not closing the database at the end of your dcadd alias. Not sure, but I suspect that might be the problem.
|
|
|
|
aslanthekat Novice
Joined: 17 Sep 2007 Posts: 44 Location: Missouri
|
Posted: Fri Jul 08, 2011 5:32 pm |
I'm not exactly sure what the correct syntax is, but I have tried
Code: |
#call @mydb.close()
and
#call @mydb.close |
So it is entirely possible that I just don't know the correct syntax.
And just to be clearer, the error I get about ent 1... Thats what I'm using with the dcquery alias. Other options like dcquery cen 3 results in the same error but says cen 3 instead of ent 1 |
|
|
|
Daern Sorcerer
Joined: 15 Apr 2011 Posts: 809
|
Posted: Fri Jul 08, 2011 6:07 pm |
The #SQLCLOSE command...you do it in your dcquery alias...
|
|
|
|
aslanthekat Novice
Joined: 17 Sep 2007 Posts: 44 Location: Missouri
|
Posted: Fri Jul 08, 2011 6:30 pm |
using #sqlclose gives me an invalid pointer on the dcadd alias
|
|
|
|
aslanthekat Novice
Joined: 17 Sep 2007 Posts: 44 Location: Missouri
|
Posted: Wed Jul 13, 2011 12:41 am |
I've been throwing some things at this for the last few days and haven't had any luck. If anyone else has any suggestions I'd appreciate them.
|
|
|
|
|
|