Parameterized SQL statement: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 324: | Line 324: | ||
You'll need an instance of a type with an instance for the <tt>IConnection</tt> type class in order to use this function, such as [http://hackage.haskell.org/package/HDBC-postgresql-2.3.2.5/docs/Database-HDBC-PostgreSQL.html#t:Connection <tt>Connection</tt>] from [http://hackage.haskell.org/package/HDBC-postgresql <tt>HDBC-postgresql</tt>]. |
You'll need an instance of a type with an instance for the <tt>IConnection</tt> type class in order to use this function, such as [http://hackage.haskell.org/package/HDBC-postgresql-2.3.2.5/docs/Database-HDBC-PostgreSQL.html#t:Connection <tt>Connection</tt>] from [http://hackage.haskell.org/package/HDBC-postgresql <tt>HDBC-postgresql</tt>]. |
||
=={{header|Huginn}}== |
|||
<lang huginn>import Database as db; |
|||
import Algorithms as algo; |
|||
import FileSystem as fs; |
|||
main() { |
|||
dbPath = "/tmp/parametrized-sql.sqlite"; |
|||
fs.remove( dbPath ); |
|||
fs.open( dbPath, fs.OPEN_MODE.WRITE ); |
|||
conn = db.connect( "sqlite3:///" + dbPath ); |
|||
// Setup... |
|||
conn.query( |
|||
"CREATE TABLE Players (\n" |
|||
"\tname VARCHAR(64),\n" |
|||
"\tscore FLOAT,\n" |
|||
"\tactive INTEGER,\n" |
|||
"\tno VARCHAR(8)\n" |
|||
");" |
|||
).execute(); |
|||
conn.query( |
|||
"INSERT INTO Players VALUES ( 'name', 0, 'false', 99 );" |
|||
).execute(); |
|||
conn.query( |
|||
"INSERT INTO Players VALUES ( 'name', 0, 'false', 100 );" |
|||
).execute(); |
|||
// Demonstrate parameterized SQL... |
|||
parametrizedQuery = conn.query( |
|||
"UPDATE Players SET name=?, score=?, active=? WHERE no=?" |
|||
); |
|||
for ( i, v : algo.enumerate( ( "Smith, Steve", 42, true, 99 ) ) ) { |
|||
parametrizedQuery.bind( i + 1, string( v ) ); |
|||
} |
|||
parametrizedQuery.execute(); |
|||
// and show the results... |
|||
for ( record : conn.query( "SELECT * FROM Players;" ).execute() ) { |
|||
print( "{}\n".format( record ) ); |
|||
} |
|||
return ( 0 ); |
|||
}</lang> |
|||
=={{header|Java}}== |
=={{header|Java}}== |