| from sqlalchemy import Column, String, create_engine,Integer |
| from sqlalchemy.orm import sessionmaker,declarative_base |
| |
| engine = create_engine('sqlite:///le00.db') |
| session =sessionmaker(bind=engine)() |
| |
| |
| Base =declarative_base() |
| |
| class User(Base): |
| |
| __tablename__ = 'user' |
| |
| username = Column(String,primary_key=True) |
| age = Column(Integer) |
| |
| def __init__(self,username,age) -> None: |
| self.username = username |
| self.age = age |
| |
| |
| alice = User('alice',10) |
| bruce = User('bruce',20) |
| session.add(alice) |
| session.add(bruce) |
| session.commit() |
| |
| |
| |
declarative_base导入路径变化
| |
| (venv) song@songdeMBP sqlalchemy_learn_20230210 % python3 le00.py |
| |
| /Users/song/Code/sqlalchemy_learn_20230210/le00.py:9: MovedIn20Warning: The ``declarative_base()`` function is now |
| available as sqlalchemy.orm.declarative_base(). (deprecated since: 2.0) (Background on SQLAlchemy 2.0 at: |
| https://sqlalche.me/e/b8d9) |
| Base =declarative_base() |
| |
sqlite3.OperationalError: no such table: user
| Traceback (most recent call last): |
| File "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line |
| 1964, in _exec_single_context |
| self.dialect.do_execute( |
| File "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/engine/default.py", line |
| 747, in do_execute |
| cursor.execute(statement, parameters) |
| sqlite3.OperationalError: no such table: user |
| |
| The above exception was the direct cause of the following exception: |
| |
| Traceback (most recent call last): |
| File "/Users/song/Code/sqlalchemy_learn_20230210/le00.py", line 25, in <module> |
| session.commit() |
| File "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/orm/session.py", line |
| 1903, in commit |
| trans.commit(_to_root=True) |
| File "<string>", line 2, in commit |
| File |
| "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/orm/state_changes.py", |
| line 137, in _go |
| ret_value = fn(self, *arg, **kw) |
| ^^^^^^^^^^^^^^^^^^^^ |
| File "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/orm/session.py", |
| line 1218, in commit |
| self._prepare_impl() |
| File "<string>", line 2, in _prepare_impl |
| File |
| "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/orm/state_changes.py", |
| line 137, in _go |
| ret_value = fn(self, *arg, **kw) |
| ^^^^^^^^^^^^^^^^^^^^ |
| File |
| "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/orm/session.py", |
| line 1193, in _prepare_impl |
| self.session.flush() |
| File |
| "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/orm/session.py", |
| line 4140, in flush |
| self._flush(objects) |
| File |
| "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/orm/session.py", |
| line 4276, in _flush |
| with util.safe_reraise(): |
| File |
| "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/util/langhelpers.py", |
| line 147, in __exit__ |
| raise exc_value.with_traceback(exc_tb) |
| File |
| "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/orm/session.py", |
| line 4237, in _flush |
| flush_context.execute() |
| File |
| "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/orm/unitofwork.py", |
| line 467, in execute |
| rec.execute(self) |
| File |
| "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/orm/unitofwork.py", |
| line 644, in execute |
| util.preloaded.orm_persistence.save_obj( |
| File |
| "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/orm/persistence.py", |
| line 93, in save_obj |
| _emit_insert_statements( |
| File |
| "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/orm/persistence.py", |
| line 1024, in _emit_insert_statements |
| result = connection.execute( |
| ^^^^^^^^^^^^^^^^^^^ |
| File |
| "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/engine/base.py", |
| line 1414, in execute |
| return meth( |
| (venv) song@songdeMBP sqlalchemy_learn_20230210 % python3 le00.py |
| Traceback (most recent call last): |
| File |
| "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/engine/base.py", |
| line 1964, in _exec_single_context |
| self.dialect.do_execute( |
| File |
| "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/engine/default.py", |
| line 747, in do_execute |
| cursor.execute(statement, parameters) |
| sqlite3.OperationalError: no such table: user |
| |
| The above exception was the direct cause of the following exception: |
| |
| Traceback (most recent call last): |
| File "/Users/song/Code/sqlalchemy_learn_20230210/le00.py", line 24, in <module> |
| session.commit() |
| |
sqlite 的命令
| (venv) song@songdeMBP sqlalchemy_learn_20230210 % sqlite3 le00.db |
| SQLite version 3.39.5 2022-10-14 20:58:05 |
| Enter ".help" for usage hints. |
| sqlite> .db |
| Error: unknown command or invalid arguments: "db". Enter ".help" for help |
| |
.help
| sqlite> .help |
| .auth ON|OFF Show authorizer callbacks |
| .backup ?DB? FILE Backup DB (default "main") to FILE |
| .bail on|off Stop after hitting an error. Default OFF |
| .binary on|off Turn binary output on or off. Default OFF |
| .cd DIRECTORY Change the working directory to DIRECTORY |
| .changes on|off Show number of rows changed by SQL |
| .check GLOB Fail if output since .testcase does not match |
| .clone NEWDB Clone data into NEWDB from the existing database |
| .connection [close] [#] Open or close an auxiliary database connection |
| .databases List names and files of attached databases |
| .dbconfig ?op? ?val? List or change sqlite3_db_config() options |
| .dbinfo ?DB? Show status information about the database |
| .dump ?OBJECTS? Render database content as SQL |
| .echo on|off Turn command echo on or off |
| .eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN |
| .excel Display the output of next command in spreadsheet |
| .exit ?CODE? Exit this program with return-code CODE |
| .expert EXPERIMENTAL. Suggest indexes for queries |
| .explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto |
| .filectrl CMD ... Run various sqlite3_file_control() operations |
| .fullschema ? |
| .headers on|off Turn display of headers on or off |
| .help ?-all? ?PATTERN? Show help text for PATTERN |
| .hex-rekey OLD NEW NEW Change the encryption key using hexadecimal |
| .import FILE TABLE Import data from FILE into TABLE |
| .import FILE TABLE Import data from FILE into TABLE |
| .imposter INDEX TABLE Create imposter table TABLE on index INDEX |
| .indexes ?TABLE? Show names of indexes |
| .limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT |
| .lint OPTIONS Report potential schema issues. |
| .log FILE|off Turn logging on or off. FILE can be stderr/stdout |
| .mode MODE ?OPTIONS? Set output mode |
| .nonce STRING Suspend safe mode for one command if nonce matches |
| .nullvalue STRING Use STRING in place of NULL values |
| .once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE |
| .open ?OPTIONS? ?FILE? Close existing database and reopen FILE |
| .output ?FILE? Send output to FILE or stdout if FILE is omitted |
| .parameter CMD ... Manage SQL parameter bindings |
| .print STRING... Print literal STRING |
| .progress N Invoke progress handler after every N opcodes |
| .prompt MAIN CONTINUE Replace the standard prompts |
| .quit Exit this program |
| .read FILE Read input from FILE or command output |
| .rekey OLD NEW NEW Change the encryption key |
| .recover Recover as much data as possible from corrupt db. |
| .restore ?DB? FILE Restore content of DB (default "main") from FILE |
| .save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...) |
| .scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off |
| .schema ?PATTERN? Show the CREATE statements matching PATTERN |
| .selftest ?OPTIONS? Run tests defined in the SELFTEST table |
| .separator COL ?ROW? Change the column and row separators |
| .session ?NAME? CMD ... Create or control sessions |
| .sha3sum ... Compute a SHA3 hash of database content |
| .shell CMD ARGS... Run CMD ARGS... in a system shell |
| # sqlite 创建数据库 |
| |
| (venv) song@songdeMBP sqlalchemy_learn_20230210 % sqlite3 le00.db |
| SQLite version 3.39.5 2022-10-14 20:58:05 |
| Enter ".help" for usage hints. |
| sqlite> .tables |
| sqlite> create table user(username text primary key,age int); |
| sqlite> .tables |
| user |
| sqlite> .quit |
| |
sqlite3.IntegrityError: UNIQUE constraint failed: user.username
| |
| (venv) song@songdeMBP sqlalchemy_learn_20230210 % python3 le00.py |
| |
| Traceback (most recent call last): |
| File |
| "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/engine/base.py", |
| line 1933, in _exec_single_context |
| self.dialect.do_executemany( |
| File |
| "/Users/song/Code/sqlalchemy_learn_20230210/venv/lib/python3.11/site-packages/sqlalchemy/engine/default.py", |
| line 744, in do_executemany |
| cursor.executemany(statement, parameters) |
| sqlite3.IntegrityError: UNIQUE constraint failed: user.username |
| |
| The above exception was the direct cause of the following exception: |
| |
| Traceback (most recent call last): |
| File "/Users/song/Code/sqlalchemy_learn_20230210/le00.py", line 28, in |
| <module> |
| session.commit() |
| |
导出shell命令的历史记录
| % history 0 > shell_history.txt |
| (venv) song@songdeMBP sqlalchemy_learn_20230210 |
| |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
2022-02-10 for循环中使用异步请求的注意
2022-02-10 promise 成功回调 失败回调
2022-02-10 reduce中使用request请求
2022-02-10 常见的数据操作 封装成一个对象处理
2022-02-10 nodejs中的request请求 promise封装
2022-02-10 nodejs的request请求 解码设置
2022-02-10 const关键字在for循环中的使用