首页 > 数据库 >sqlalchemy_sqlite_shellhistory

sqlalchemy_sqlite_shellhistory

时间:2023-02-10 23:23:02浏览次数:44  
标签:sqlite sqlalchemy 20230210 song py venv learn shellhistory

/Users/song/Code/sqlalchemy_learn_20230210/le00.py

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 ?--indent? Show schema and the content of sqlite_stat tables
.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

标签:sqlite,sqlalchemy,20230210,song,py,venv,learn,shellhistory
From: https://www.cnblogs.com/zhuoss/p/17110633.html

相关文章

  • sqlite数据库死亡提示database disk image is malformed的修复经历
    今天在处理一个客户的问题时,发现使用客户的sqlite数据库,本地调试代码报databasediskimageismalformed,查了下说是文件损坏了,网上有修复的方法,说实话开始也不抱太大......
  • Android sqlite3工具的使用
    sqlite3<数据库名称>进入数据库操作模式eg:sqlite3contacts.db使用这条命名前,先进入到该数据库的位置(需要用adbshell),执行 sqlite3contacts.db.tables查看所有的......
  • 若依-更换数据库-sqlite
    基础我是在ruoyi-vue已经安装了mybatis-plus的基础上进行的修改关于SQLiteSQLite是一个软件库,实现了自给自足的、无服务器的、零配置的、事务性的SQL数据库引擎。SQ......
  • Blazor入门100天 : 身份验证和授权 (3) - DB改Sqlite
    目录建立默认带身份验证Blazor程序角色/组件/特性/过程逻辑DB改Sqlite将自定义字段添加到用户表脚手架拉取IDS文件,本地化资源freesql生成实体类,freesql管理......
  • 检测sqlite3数据库的完整性
    1/**2*@integrityCheck3*@brief检查数据库的完整性4*5*@paramdbPath数据库的路径6*@返回值0:完整;-1损坏7*/8intintegrityCheck(......
  • flask_migrate---处理 sqlalchemy 数据迁移的工具
    1.什么是flask_migrateflask_migrate是专门用来做sqlalchemy数据迁移的工具,当据模型发生变化的时可将修改后的模型重新映射到数据库中,这意味着数据库也将被修改。本文介......
  • SQLite 初试
    今天需要用到 SQLite统计数据,简单总结一下,SQLite是一个轻量级数据库,语法与mysql相似,不需要安装,解压即可使用。 下载地址:​​https://www.sqlite.org/download.html​......
  • Compile Sqlite3 Executable, Static Library, and Shared Library on Linux
    DownloadSqlite3sourcecode,anddecompressittosomewhere.Enterthedecompressedfolder,typethefollowingcommandtogeneratedifferenttargetswithall......
  • How SQLite Is Tested
    https://www.sqlite.org/testing.html8.2. ValgrindValgrind isperhapsthemostamazingandusefuldevelopertoolintheworld.Valgrindisasimulator-its......
  • QSqlite 使用记录
    QSqlite使用记录一个QSqlDatabase的实例表示着一个对数据库的连接setDatabaseName("");指定文件路径,就可以操作指定的文件插入sqlite在没有显式使用事务的时候会为每......