mysql 的基本表的应用_数据库的基本应用

数据库的基本应用:

主键 : 唯一 ,不能重复 ,不能为空 , 可以同时当外键

外键 : 将两个表关联起来 , 不能重复

1.数据插入命令 : insert(增加)

NSERT INTO `Demo_Table`(`demo_id`, `demo_name`) VALUES (1,'xinxin');

2.数据更新命令 : update( 修改)

UPDATE `Demo_Table` SET `demo_name` = 'longlong' WHERE `demo_id`=1;

如果要修改两个或多个值中间用  逗号  隔开.

3.数据删除命令 :  delete(删除)

DELETE FROM `Demo_Table` WHERE `demo_id`=1;

4.数据查命令 : select (查询)

SELECT * FROM `Demo_Table` WHERE `demo_id`=1;

二、表操作,操作之前应连接某个数据库

1、建表

命令:create table ( [,.. ]);

mysql> create table MyClass(

> id int(4) not null primary key auto_increment,

> name char(20) not null,

> sex int(4) not null default ''0'',

> degree double(16,2));

2、获取表结构

命令: desc 表名,或者show columns from 表名

mysql>DESCRIBE MyClass

mysql> desc MyClass;

mysql> show columns from MyClass;

3、删除表

命令:drop table

例如:删除表名为 MyClass 的表mysql> drop table MyClass;

4、插入数据

命令:insert into [( [,.. ])] values ( 值1 )[, ( 值n )]

例如,往表 MyClass中插入二条记录, 这二条记录表示:编号为1的名为Tom的成绩为96.45, 编号为2 的名为Joan 的成绩为82.99,编号为3 的名为Wang 的成绩为96.5.

mysql> insert into MyClass values(1,'Tom',96.45),(2,'Joan',82.99), (2,'Wang', 96.59);

5、查询表中的数据

1)、查询所有行

命令: select from < 表名 > where < 表达式 >

例如:查看表 MyClass 中所有数据mysql> select * from MyClass;

2)、查询前几行数据

例如:查看表 MyClass 中前2行数据

mysql> select * from MyClass order by id limit 0,2;

6、删除表中数据

命令:delete from 表名 where 表达式

例如:删除表 MyClass中编号为1 的记录

mysql> delete from MyClass where id=1;

7、修改表中数据:update 表名 set 字段=新值,… where 条件

mysql> update MyClass set name=''Mary'' where id=1;

8、在表中增加字段:

命令:alter table 表名 add 字段 类型 其他;

例如:在表MyClass中添加了一个字段passtest,类型为int(4),默认值为0

mysql> alter table MyClass add passtest int(4) default ''0''

9、更改表名:

命令:rename table 原表名 to 新表名;

例如:在表MyClass名字更改为YouClass

mysql> rename table MyClass to YouClass;

更新字段内容

update 表名 set 字段名 = 新内容

update 表名 set 字段名 = replace(字段名,''旧内容'',''新内容'');

#import "StudentManager.h"

#import#import

@class Student;

@interface StudentManager : NSObject

@property(nonatomic , retain) NSString *tableName;

+ (NSString *)documentsWithName:(NSString *)name;

+ (StudentManager *)shareManager;

- (void)insertWithStudent:(Student *)stu;

- (void)deleteWithId:(NSInteger)stuId;

- (void)updateWithStudent:(Student *)stu;

- (void)openWithTable:(NSString *)tableName;

- (void)createTable:(NSString *)tableName;

- (void)close;

@end

#import "Student.h"

//单例全局变量,默认为空

static StudentManager *manager = nil;

//定义数据库指针对象

static sqlite3 *dbPoint = nil;

@implementation StudentManager

//创建一个单例

+ (StudentManager *)shareManager

{

//声明一个GCD全局变量

static dispatch_once_t onceToken;

//块语法中的内容只执行一次,线程安全,也叫单利安全

dispatch_once(&onceToken, ^{

// 保证内存唯一

if (manager == nil) {

manager = [[StudentManager alloc] init];

}

});

return manager;

}

//    下面全都是单利

//    [UIDevice currentDevice];

//    [UIApplication sharedApplication];

//    [NSUserDefaults standardUserDefaults];

//    [NSNotificationCenter defaultCenter];  //消息中心

//创建数据库文件并打开

- (void)openWithTable:(NSString *)tableName

{

self.tableName = tableName;

NSString *path = [StudentManager documentsWithName:@"Student.sqlite"];

NSLog(@"pat == %@",path);

//打开数据库,并获得数据库对象

//sqlite3_open 如果没有这个就创建一个,如果有就打开

int result = sqlite3_open([path UTF8String], &dbPoint);

if (result != SQLITE_OK) {

UIAlertView  *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"数据库打开失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

[alter show];

[alter release];

}else{

NSLog(@"打开成功");

}

[self createTable:tableName];

}

//创建表

- (void)createTable:(NSString *)tableName

{

NSString *sqlStr = [NSString stringWithFormat:@"create table %@ (stuid int , stuname text,stuscore float)" , tableName];

//sqlite3_exec执行当前的sql语句

int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], NULL, NULL, NULL);

if (result == SQLITE_OK) {

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"表创建成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

[alert show];

[alert release];

}else{

NSLog(@"增加失败");

}

}

//关闭数据库

- (void)close

{

sqlite3_close(dbPoint);

NSLog(@"关闭成功");

}

//增加信息

- (void)insertWithStudent:(Student *)stu

{

NSString *sqlStr = [NSString stringWithFormat:@"insert into %@ values(%d , '%@', %g)",_tableName , stu.stuId, stu.stuName , stu.stuScore];

int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], NULL, NULL, NULL);

if (result == SQLITE_OK) {

UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"增加成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

[alter show];

[alter release];

} else{

NSLog(@"增加失败");

}

}

//删除信息

- (void)deleteWithId:(NSInteger)stuId

{

NSString *sqlStr = [NSString stringWithFormat:@"delete from '%@' where stuId = %d",_tableName , stuId];

int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], NULL, NULL, NULL);

if (result == SQLITE_OK) {

UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"删除成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

[alter show];

[alter release];

}else {

NSLog(@"删除失败");

}

}

//修改信息

- (void)updateWithStudent:(Student *)stu

{

NSString *sqlStr = [NSString stringWithFormat:@"update '%@' set stuName = '%@' , stuScore = %g where stuID = %d",_tableName , stu.stuName ,stu.stuScore,stu.stuId];

//声明一个结果

int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], NULL, NULL, NULL);

if (result == SQLITE_OK) {

UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"修改成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

[alter show];

[alter release];

}else {

NSLog(@"修改失败");

}

}

//查询信息

- (NSArray *)selectStuWithScore:(CGFloat)score

{

NSString *sqlStr = [NSString stringWithFormat:@"select * from '%@' where stuscore >= %g ",self.tableName ,score];

//数据库对象的替身

sqlite3_stmt *stmt = nil;

//最后一个参数代表前面写的哪句话是不用的,没用就用null

int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, NULL);

NSMutableArray *arr = [NSMutableArray array];

if (result == SQLITE_OK) {

//循环判断 查找是否有下一行数据,并且判断,如果有了,取出放到数组中

while (sqlite3_step(stmt) == SQLITE_ROW) {

int stuid = sqlite3_column_int(stmt, 0);

const unsigned char *stuname = sqlite3_column_text(stmt, 1);

float stuscore = sqlite3_column_double(stmt, 2);

Student *stu = [Student studentWithName:[NSString stringWithUTF8String:(const char*)stuname ]stuId:stuid stuScore:stuscore];

[arr addObject:stu];

}

}

//将替身清空

sqlite3_finalize(stmt);

return arr;

}

//返回数据库文件路径

+ (NSString *)documentsWithName:(NSString *)name

{

NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

NSString * path = [NSString stringWithFormat:@"%@/%@" , doc , name];

return path;

}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值