一、前言
最近开始入门python,当然是要使用PyCharm,然后在项目中遇到.db数据库文件,双击打不开?网上找到了windows版本的教程,版本也比较旧,所以有空就来一发,当备忘也好~
二、链接SQLite
2.1 控制台创建数据库DB文件
![img_b889ffb1f336a6dbc07dfc5e804b4249.png](https://yqfile.alicdn.com/img_b889ffb1f336a6dbc07dfc5e804b4249.png?x-oss-process=image/resize,w_1400/format,webp)
创建数据库DB文件
2.2 打开sqlite配置界面
按下图步骤打开sqlite配置目录
![img_6c21dee48047aed38d84f74d774c3f7f.png](https://yqfile.alicdn.com/img_6c21dee48047aed38d84f74d774c3f7f.png?x-oss-process=image/resize,w_1400/format,webp)
打开sqlite配置界面
2.3 安装sqlite驱动
点击下载驱动,直到显示提示“no objects”:
![img_50d8f4e725f4e02380ffc13450a6c386.png](https://yqfile.alicdn.com/img_50d8f4e725f4e02380ffc13450a6c386.png?x-oss-process=image/resize,w_1400/format,webp)
安装sqlite驱动
2.4 链接刚才创建的数据库 ios_private.db文件
![img_42bbdb4fdda0092a38b312cc9f81f754.png](https://yqfile.alicdn.com/img_42bbdb4fdda0092a38b312cc9f81f754.png?x-oss-process=image/resize,w_1400/format,webp)
链接数据库Sqlite文件
点击步骤4的“Test Connection” 时,显示 Successful
就表示连接成功,点击右下角的OK返回!
![img_d88fa2a02f70c79516b2846981fce43a.png](https://yqfile.alicdn.com/img_d88fa2a02f70c79516b2846981fce43a.png?x-oss-process=image/resize,w_1400/format,webp)
Test Connection
PyCharm自动打开Sqlite数据库:
![img_8f061f1e85dd188fff86e68f054cf248.png](https://yqfile.alicdn.com/img_8f061f1e85dd188fff86e68f054cf248.png?x-oss-process=image/resize,w_1400/format,webp)
SqliteConsole
三、操作数据库
3.1 创建一张表
![img_0e59e3df33685e4059d2316ce94aae13.png](https://yqfile.alicdn.com/img_0e59e3df33685e4059d2316ce94aae13.png?x-oss-process=image/resize,w_1400/format,webp)
createTable
![img_b40848cd81bb8948966f419464ceb9e9.png](https://yqfile.alicdn.com/img_b40848cd81bb8948966f419464ceb9e9.png?x-oss-process=image/resize,w_1400/format,webp)
createTableConsole
3.2 添加数据
![img_f14996b8625131f3080ee5dc265917de.png](https://yqfile.alicdn.com/img_f14996b8625131f3080ee5dc265917de.png?x-oss-process=image/resize,w_1400/format,webp)
insertIntoTable
3.3 查询数据
![img_dc94c6357254a430898fff3c048bf6de.png](https://yqfile.alicdn.com/img_dc94c6357254a430898fff3c048bf6de.png?x-oss-process=image/resize,w_1400/format,webp)
SelectTable
3.4 关联表
![img_eeb258575a9c2034d58bcb84d602b19d.png](https://yqfile.alicdn.com/img_eeb258575a9c2034d58bcb84d602b19d.png?x-oss-process=image/resize,w_1400/format,webp)
referencesForeignKey
3.5 更多示例
![img_e90966218c849d20b0e2d2f4f6d1f221.png](https://yqfile.alicdn.com/img_e90966218c849d20b0e2d2f4f6d1f221.png?x-oss-process=image/resize,w_1400/format,webp)
insertValues
相关代码:
-- 创建一个表
create table iOSDevice(
deviceName TEXT
);
-- 插入数据
insert into iOSDevice values('iPhone8');
insert into iOSDevice values('iPhone8 Plus');
insert into iOSDevice values('iPhoneX');
-- 查询所有数据
select * from iOSDevice;
-- 创建一个新表
create table iProduct (
Mac text,
iPhone text,
iPad text,
Watch text,
-- 关联表
foreign key (iPhone) references iOSDevice(deviceName)
)
-- 插入多个值
insert into iProduct values(
'Macbook Pro',
'iPhone',
'iPad mini4',
'apple Watch'
);
-- 查询表数据
select * from iProduct;
四、总结
通过PyCharm进行SQLite操作,之前真没有想到PyCharm如此强大!IDE就是 IDE,收费也是硬道理!希望好好利用PyCharm做更多有趣的事件~
五、参考引用
注:本文首发于 iHTCboy's blog,如若转载,请注明来源。