And there is the body of the while loop:
(while (?result next)
(?statement execute "insert into test (persen) values (?result
getString y)"))
which looks strange to me, as the string literal contains (presumably)
some SQL statement, where the embedded Jess "(?result getString y)" may
be, uh, optimistic. An SQL insert statement would look like
insert into test (persen) values ('blue green')
and I guess you'd have to compose this, in Jess, as a string
(bind ?sql (str-cat "insert into test (persen) values ('" (?result
getString y) "')" )
so that you could do the glorious
(?statement execute ?sql)
inside the while loop.
Regards
Wolfgang
Ernest Friedman-Hill wrote:
A QueryResult is like an Iterator. It has a sort of "cursor" inside,
and calling "next()" advances that cursor. When the cursor gets to
the end, then the Iterator can't be used anymore and must be
discarded. In your example below, you use the QueryResult to loop
over the results and print them out, but then you have a second loop
which tries to use the QueryResult again. Because the cursor is now
at the end, "next" immediately returns false and that loop doesn't
execute. Remove that first loop and this should work (unless you've
made other mistakes that I'm not seeing.)
On Dec 17, 2007, at 9:17 AM, Barlianti Vavorita wrote:
hello,
can i insert data to database through JESS? i want to insert data on
working memory to the database. first i used defquery to choose data
which
i want to insert into database.
i have tried to select data in database with JESS and i can do it
successfully. but why when i tried to insert data i failed? im using
microsoft access 2003.
this is the code when i select data :
Jess> (import java.sql.*)
TRUE
Jess> (call Class forName sun.jdbc.odbc.JdbcOdbcDriver)
<Java-Object:java.lang.Class>
Jess> (bind ?conn (call DriverManager getConnection
"jdbc:odbc:databaseTest"))
<Java-Object:sun.jdbc.odbc.JdbcOdbcConnection>
Jess> (bind ?statement (?conn createStatement))
<Java-Object:sun.jdbc.odbc.JdbcOdbcStatement>
Jess> (bind ?rs (?statement executeQuery "select * from pakar"))
<Java-Object:sun.jdbc.odbc.JdbcOdbcResultSet>
Jess> (while (?rs next) do
(printout t (?rs getString nama) " " (?rs getString jabatan) crlf))
java pakar
jess pakar
test conn pakar
hello world pakar
john doe pakar
tom riddle pakar
vivi pakar
admin admin
FALSE
Jess> (?statement close)
Jess> (?conn close)<br>
Jess> <br>
Jess>
and this is when i insert data, if i wrong in syntax, can you help
me to
fix it.
Jess> (defquery search
(declare (variables ?x))
(color ?x ?y))
TRUE
Jess> (deffacts data
(color blue red)
(color blue green)
(color blue pink)
(color red blue)
(color blue blue)
(color orange yellow)
(warna blue purple))
TRUE
Jess> (reset)
TRUE
Jess> (facts)
f-0 (MAIN::initial-fact)
f-1 (MAIN::color blue red)
f-2 (MAIN::color blue green)
f-3 (MAIN::color blue pink)
f-4 (MAIN::color red blue)
f-5 (MAIN::color blue blue)
f-6 (MAIN::color orange yellow)
f-7 (MAIN::warna blue purple)
For a total of 8 facts in module MAIN.
Jess> (bind ?result (run-query* search blue))
<Java-Object:jess.QueryResult>
Jess> (while (?result next)
(printout t (?result getString x)" "(?result getString y) crlf))
blue red
blue green
blue pink
blue blue
FALSE
Jess> (import java.sql.*)
TRUE
Jess> (call Class forName sun.jdbc.odbc.JdbcOdbcDriver)
<Java-Object:java.lang.Class>
Jess> (bind ?conn (call DriverManager getConnection
"jdbc:odbc:databaseTest"))
<Java-Object:sun.jdbc.odbc.JdbcOdbcConnection>
Jess> (bind ?statement (?conn createStatement))
<Java-Object:sun.jdbc.odbc.JdbcOdbcStatement>
Jess> (bind ?result (run-query* search blue))
<Java-Object:jess.QueryResult>
Jess> (while (?result next)
(printout t (?result getString x)" "(?result getString y) crlf))
blue red
blue green
blue pink
blue blue
FALSE
Jess> (facts)
f-0 (MAIN::initial-fact)
f-1 (MAIN::color blue red)
f-2 (MAIN::color blue green)
f-3 (MAIN::color blue pink)
f-4 (MAIN::color red blue)
f-5 (MAIN::color blue blue)
f-6 (MAIN::color orange yellow)
f-7 (MAIN::warna blue purple)
For a total of 8 facts in module MAIN.
Jess> (while (?result next)
(?statement execute "insert into test (persen) values (?result
getString
y)"))
FALSE
Jess>
and the content in database is still blank.
in different way i got this error :
Jess> (while (?result next)
(?statement execute "insert into test (persen) values (?result
getString
z)"))
Jess reported an error in routine Context.getReturn
while executing (call ?statement execute "insert into test
(persen) valu
es (?result getString z)")
while executing (while (call ?result next) (call ?statement
execute "ins
ert into test (persen) values (?result getString z)")).
Message: No such variable statement.
Program text: ( while ( ?result next ) ( ?statement execute
"insert into
test
(persen) values (?result getString z)" ) ) at line 282.
please help me to fix it, thank you.
regards,
vivi
--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify owner-jess-
[EMAIL PROTECTED]
--------------------------------------------------------------------
---------------------------------------------------------
Ernest Friedman-Hill
Informatics & Decision Sciences Phone: (925) 294-2154
Sandia National Labs FAX: (925) 294-2234
PO Box 969, MS 9012 [EMAIL PROTECTED]
Livermore, CA 94550 http://www.jessrules.com
--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify
[EMAIL PROTECTED]
--------------------------------------------------------------------
--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]
--------------------------------------------------------------------