magit / emacsql

A high-level Emacs Lisp RDBMS front-end
The Unlicense
554 stars 41 forks source link

syntax question of update statement. #39

Closed zhanhe18 closed 6 years ago

zhanhe18 commented 6 years ago

Hi , i've been trying a couple of hours about update statement and always get syntax error . i don't know where i did wrong . my code : (emacsql db [:update test_table :set state=$s1 :where (= name $s2) ] 2 "table_name") or maybe (emacsql db [:update test_table :set (state $s1) :where (=name $s2) ] 2 "table_name") Could you offer me some update statment examples? thank you.

skeeto commented 6 years ago

If I understand what you're trying to do, you're close. You can use "M-x emacsql-show-last-sql" with the point after the EmacSQL expression to see how it compiles to SQL.

Remember that in Emacs Lisp symbols can be made up of any character, including things that look like operators in other languages. For example "state=$s1" is a symbol named exactly that. The Elisp parser doesn't break that up into three separate tokens. You must use whitespace.

The second thing is that all operators are lisp style, including the equality operator =. You have the right idea with "name" in the second part. Here's what I think you want:

[:update test_table :set (= state $s1) :where (= name $s2)] 

Which compiles to:

UPDATE test_table SET state = $1 WHERE name = $2;

Where $1 and $2 are the bind parameters.

Finally, just as a style note, the more lispy name for the table is "test-table", which the compiler will automatically convert to "test_table". This means the following expression is equivalent while looking more lispy:

[:update test-table :set (= state $s1) :where (= name $s2)] 
zhanhe18 commented 6 years ago

thank you very much . and thank you for your words about symbols.

zhanhe18 commented 6 years ago

thank you very much . and thank you for your words about symbols.

dustinlacewell commented 4 years ago

Should probably add this to both the README and the tests.