|
20 | 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 | 21 | # SOFTWARE.
|
22 | 22 |
|
23 |
| -# import sqlalchemy |
24 |
| -# from sqlalchemy import Table, Column, String, MetaData |
25 |
| -# |
26 |
| -# engine = sqlalchemy.create_engine('postgres://admin:123456@localhost:5432/spotrix') |
| 23 | +import sqlalchemy |
| 24 | +from sqlalchemy import Table, Column, String, MetaData |
| 25 | + |
| 26 | +engine = sqlalchemy.create_engine('postgresql://sa:sa@localhost:5435/xxx') |
| 27 | + |
| 28 | +with engine.connect() as connection: |
| 29 | + metadata = MetaData() |
| 30 | + tables = Table('tables', metadata, |
| 31 | + Column('table_schema', String), |
| 32 | + Column('table_name', String), |
| 33 | + schema='information_schema') |
| 34 | + results = connection.execute( |
| 35 | + tables.select().where(tables.c.table_schema == 'pg_catalog')) |
| 36 | + for result in results: |
| 37 | + print(result) |
| 38 | + |
| 39 | +# Create |
| 40 | +engine.execute("CREATE TABLE IF NOT EXISTS films (title text, director text, year_yy text)") |
| 41 | +engine.execute("INSERT INTO films (title, director, year_yy) VALUES ('Doctor Strange', 'Scott Derrickson', '2016')") |
| 42 | + |
| 43 | +# Read |
| 44 | +result_set = engine.execute("SELECT * FROM films") |
| 45 | +for r in result_set: |
| 46 | + print(r) |
| 47 | + |
| 48 | +# Update |
| 49 | +engine.execute("UPDATE films SET title='Some2016Film' WHERE year_yy='2016'") |
| 50 | + |
| 51 | +# Delete |
| 52 | +engine.execute("DELETE FROM films WHERE year_yy='2016'") |
| 53 | + |
| 54 | + |
| 55 | +# import duckdb |
27 | 56 | #
|
28 |
| -# with engine.connect() as connection: |
29 |
| -# metadata = MetaData() |
30 |
| -# tables = Table('tables', metadata, |
31 |
| -# Column('table_schema', String), |
32 |
| -# Column('table_name', String), |
33 |
| -# schema = 'information_schema') |
34 |
| -# results = connection.execute(tables.select().where(tables.c.table_schema=='pg_catalog')) |
35 |
| -# for result in results: |
36 |
| -# print(result) |
37 |
| - |
38 |
| -import duckdb |
39 |
| - |
40 |
| -cursor = duckdb.connect() |
41 |
| -print(cursor.execute('SELECT 42').fetchall()) |
| 57 | +# cursor = duckdb.connect() |
| 58 | +# print(cursor.execute('SELECT 42').fetchall()) |
0 commit comments