db数据库利用第三方框架进行提取和解析数据

db的数据包用从github上下载的三方框架进行解析和数据提取,格式一般为数组和字典。db的查看工具是firefox上的解析db插件SQLite

三方框架为FMDB

#import "ViewController.h"

//1. 引入头文件,需要引入libsqlite3的库
#import "FMDB.h"

#import "Word.h"

@interface ViewController ()
            

@end

@implementation ViewController
            
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //2. 创建一个FMDatabase对象,并指定文件路径
    FMDatabase *database = [FMDatabase databaseWithPath:@"/Users/apple/Desktop/japanese.db"];
    //3. 打开文件
    if (![database open]) {
        NSLog(@"Can not open file");
        return;
    }
    
    //4. 执行查询
    FMResultSet *result = [database executeQuery:@"SELECT * FROM jp"];
    
    NSMutableArray *array = [NSMutableArray array];
    //5. 获取表格中的一行
    while ([result next]) {
//        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        
        Word *word = [[Word alloc] init];
        
        //根据名字(列名)获取数据
        int rid = [result intForColumn:@"id"];
        NSLog(@"rid: %d", rid);
        
        word.rid = rid;
        
//        [dict setObject:[NSNumber numberWithInt:rid] forKey:@"id"];
        
        //根据位置获取数据
        int rid1 = [result intForColumnIndex:0];
        NSLog(@"rid1: %d", rid1);
        
        NSString *kana = [result stringForColumn:@"kana"];
        NSLog(@"kana: %@", kana);
        
//        [dict setObject:kana forKey:@"kana"];
        
        NSString *kana1 = [result stringForColumnIndex:5];
        NSLog(@"kana1: %@", kana1);
        
        word.kana = kana1;
        
        [array addObject:word];
    }
    
    //6. 关闭文件
    [database close];
    
    NSLog(@"-----------");
    NSLog(@"%@", array);
}



这是word.h和word.m中的代码

#import <Foundation/Foundation.h>

@interface Word : NSObject
@property (nonatomic, assign) NSInteger rid;
@property (nonatomic, copy) NSString *kana;
@end

#import "Word.h"

@implementation Word
- (NSString *)description
{
    return [NSString stringWithFormat:@"%@: rid: %d---kana: %@", [super description], _rid, _kana];
}
@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要连接 MySQL 数据库并使用 SQL 语句进行数据提取,你需要在 Django 中进行以下配置: 1. 安装 MySQL 驱动程序 ``` pip install mysql-connector-python ``` 2. 在 `settings.py` 文件中配置数据库 ```python DATABASES = { 'default': { 'ENGINE': 'mysql.connector.django', 'NAME': 'your_database_name', 'USER': 'your_username', 'PASSWORD': 'your_password', 'HOST': 'your_host', 'PORT': 'your_port', } } ``` 其中,`your_database_name` 是你要连接的数据库名称,`your_username` 是数据库用户名,`your_password` 是数据库密码,`your_host` 是数据库主机地址,`your_port` 是数据库端口号。 3. 在应用程序的 `views.py` 文件中编写 SQL 查询语句,例如: ```python from django.db import connection def your_view(request): with connection.cursor() as cursor: cursor.execute("SELECT * FROM your_table_name;") rows = cursor.fetchall() return render(request, 'your_template.html', {'rows': rows}) ``` 其中,`your_table_name` 是你要查询的表名称,`rows` 是查询结果。 4. 在 `your_template.html` 中显示查询结果,例如: ```html <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Email</th> </tr> </thead> <tbody> {% for row in rows %} <tr> <td>{{ row.0 }}</td> <td>{{ row.1 }}</td> <td>{{ row.2 }}</td> </tr> {% endfor %} </tbody> </table> ``` 其中,`row.0`、`row.1` 和 `row.2` 分别是查询结果中的第一列、第二列和第三列。 这样,你就可以使用 Django 连接 MySQL 数据库并使用 SQL 语句进行数据提取了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值