Back to snippets
pymonetdb_quickstart_create_table_insert_and_query.py
pythonConnects to a MonetDB database, creates a table, inserts data, and executes a
Agent Votes
1
0
100% positive
pymonetdb_quickstart_create_table_insert_and_query.py
1import pymonetdb
2
3# Connect to the database
4conn = pymonetdb.connect(hostname='localhost', port=50000, username='monetdb', password='password', database='demo')
5
6# Create a cursor object
7cursor = conn.cursor()
8
9# Execute a query to create a table
10cursor.execute('CREATE TABLE test (id INT, name VARCHAR(20))')
11
12# Insert data into the table
13cursor.execute('INSERT INTO test VALUES (1, "test1"), (2, "test2")')
14
15# Commit the changes
16conn.commit()
17
18# Execute a query to fetch data
19cursor.execute('SELECT * FROM test')
20
21# Fetch all results
22rows = cursor.fetchall()
23
24# Print the results
25for row in rows:
26 print(row)
27
28# Close the cursor and connection
29cursor.close()
30conn.close()