go mysql orm_GitHub - widuu/gomysql: golang mysql orm

gomysql

68747470733a2f2f64726f6e652e696f2f6769746875622e636f6d2f77696475752f676f6d7973716c2f7374617475732e706e67

###介绍

gomysql是基于go-sql-driver基础上开发的orm,这是一个轻量级的库。它会使数据库的增删改查变得非常容易。当然也是测试开发版,会一直优化和更新!请时刻关注我们

###安装

go get github.com/go-sql-driver/mysql

go get github.com/widuu/goini

go get github.com/widuu/gomysql

###依赖的包

###使用教程

设置配置文件

[database]

username = mysql username

password = mysql password

hostname = mysql host

charset = mysql charset

database = database name

port = mysql port

初始化配置

c, _ := gomysql.SetConfig("./conf/conf.ini") //根据配置文件,连接数据库

查询数据

t := c.SetTable("user") //设置要处理的表名

data := t.FindOne() //查询表的一条数据,返回map[int]map[string]string格式

gomysql.Print(data) //打印数据信息,返回如下截图的数据格式

687474703a2f2f79756e2e77696475752e636f6d2f676f6d7973716c5f7072696e742e706e67

data := t.Fileds("id", "password", "username").Where("id =12").FindOne() //Fileds 查询字段,Where where条件FindOne()查询一条

//limit sql中的limit OrderBy sql中查询的OrderBy

data = c.SetTable("user").Fileds("id", "password", "username").Where("id>1").Limit(1, 5).OrderBy("id Desc").FindAll()

data = t.FindAll() //查询所有数据,其中OrderBy() Limit() Where() Fileds()等设置条件

gomysql.Print(data) //查询的数据都可以用Print()方法友好打印出来信息

添加数据

var value = make(map[string]interface{}) //设置map存储数据,map[key]value

value["password"] = "mima3"

value["username"] = "xiaowei"

value["id"] = 10

t := c.SetTable("user") //设置增加的数据表

t.SetPk("id") //设置主键自增的字段名称

i,err := t.Insert(value) // 插入数据,返回增加个数和错误信息。返回最后增长的id和错误信息

修改数据

var value = make(map[string]interface{})

value["password"] = "mima3"

value["username"] = "xiaowei"

n, err := c.SetTable("user").Where("id =5").Update(value) //设置表,条件和修改的内容,返回影响条数和错误信息

删除数据

n, err := c.SetTable("user").Delete("id = 6") //设置删除的表和数据,返回影响条数和错误信息

关联操作

INNER JOIN

t := c.SetTable("user")

//下面相当于sql语句,打印出是Select user.id,data.keywords,user.username,user.password from user INNER JOIN data ON user.id = data.id

data := t.Fileds("user.id", "data.keywords", "user.username", "user.password").Join("data", "user.id = data.id").FindAll()

fmt.Println(data)

LEFT JOIN

t.Fileds("user.id", "data.keywords", "user.username", "user.password").LeftJoin("data", "user.id = data.id").FindAll()

fmt.Println(data)

RIGHT JOIN

t.Fileds("user.id", "data.keywords", "user.username", "user.password").RightJoin("data", "user.id = data.id").FindAll()

fmt.Println(data)

FULL JOIN

data := t.Fileds("user.id", "data.keywords", "user.username", "user.password").RightJoin("data", "user.id = data.id").FindAll()

fmt.Println(data)

自定义sql语句

// Query()方法 是自定义sql语句的

insert类型的数据

n := t.Query("INSERT INTO user (`username`,`password`) VALUES ('xiaoweitest','xiaowei')") //返回最后增长的id

update|delete

//update

n := c.Query("update user set username='ceshishenma' where id =17 ")

fmt.Println(n) //1 返回受影响行数

//delete

n := c.Query("delete from user where id=16 ")

fmt.Println(n) //1 返回受影响行数

select

data := c.Query("select username,password from user")

fmt.Println(data) //返回map[int]map[string]string 结构的所有数据

关闭数据库

c.DbClose() //关闭数据库

##English Document

###Introduction

Gomysql is based on the go - SQL - driver based on orm, this is a lightweight library.It allows the database to add delete very easily.Of course is also testing the development version, will continue to optimize and update!Please attention to us

###Install

go get github.com/widuu/gomysql

###Rely on the package

###Using the tutorial

Set the configuration file

[database]

username = mysql username

password = mysql password

hostname = mysql host

charset = mysql charset

database = database name

port = mysql port

Initialize the configuration

c, _ := gomysql.SetConfig("./conf/conf.ini") //According to the configuration files, connect to the database

Query data

t := c.SetTable("user") //set up the table name

data := t.FindOne() //A data query,return map[int]map[string]string format

gomysql.Print(data) //Print data information, and return the following screenshots data formats

687474703a2f2f79756e2e77696475752e636f6d2f676f6d7973716c5f7072696e742e706e67

data := t.Fileds("id", "password", "username").Where("id =12").FindOne() //Fileds Query field,Where where conditions FindOne() query a data

//limit - sql limit,OrderBy - sql OrderBy

data = c.SetTable("user").Fileds("id", "password", "username").Where("id>1").Limit(1, 5).OrderBy("id Desc").FindAll()

data = t.FindAll() //Query all the data, including OrderBy () Limit () the Where () Fileds () set conditions

gomysql.Print(data) //Query data can use the Print () method of friendly printed information

Insert data

var value = make(map[string]interface{}) //Set the map data is stored, the map [key] the value

value["password"] = "mima3"

value["username"] = "xiaowei"

value["id"] = 10

t := c.SetTable("user") //Set up to increase the data tables

t.SetPk("id") //Set the primary key on the field name

i,err := t.Insert(value) // Insert data, returns the number increase and error information.Returns the last growth id and error message

Updata data

var value = make(map[string]interface{})

value["password"] = "mima3"

value["username"] = "xiaowei"

n, err := c.SetTable("user").Where("id =5").Update(value) //Set the table, the condition and modify the content of the article returns affect the number and the error information

Delete data

n, err := c.SetTable("user").Delete("id = 6") //Article, set the table and data delete, return to influence the number and the error information

Associated operation

INNER JOIN

t := c.SetTable("user")

//Equivalent to a SQL statement below, print out is

//Select user.id,data.keywords,user.username,user.password from user INNER JOIN data ON user.id = data.id

data := t.Fileds("user.id", "data.keywords", "user.username", "user.password").Join("data", "user.id = data.id").FindAll()

fmt.Println(data)

LEFT JOIN

t.Fileds("user.id", "data.keywords", "user.username", "user.password").LeftJoin("data", "user.id = data.id").FindAll()

fmt.Println(data)

RIGHT JOIN

t.Fileds("user.id", "data.keywords", "user.username", "user.password").RightJoin("data", "user.id = data.id").FindAll()

fmt.Println(data)

FULL JOIN

data := t.Fileds("user.id", "data.keywords", "user.username", "user.password").RightJoin("data", "user.id = data.id").FindAll()

fmt.Println(data)

自定义sql语句

// Query()function Is a custom SQL statement

insert

n := t.Query("INSERT INTO user (`username`,`password`) VALUES ('xiaoweitest','xiaowei')") //Return the id of the growth

update|delete

//update

n := c.Query("update user set username='ceshishenma' where id =17 ")

fmt.Println(n) //1 Return the affected rows

//delete

n := c.Query("delete from user where id=16 ")

fmt.Println(n) //1 Return the affected rows

select

data := c.Query("select username,password from user")

fmt.Println(data) //return map[int]map[string]string All of the data structure

Close the database

c.DbClose() //close the database

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
sqoop import --connect jdbc:mysql://zhaosai:3306/mydb --username root --password jqe6b6 --table news --target-dir /user/news --fields-terminated-by “;” --hive-import --hive-table news -m 1出现错误Warning: /opt/programs/sqoop-1.4.7.bin__hadoop-2.6.0/../hbase does not exist! HBase imports will fail. Please set $HBASE_HOME to the root of your HBase installation. Warning: /opt/programs/sqoop-1.4.7.bin__hadoop-2.6.0/../hcatalog does not exist! HCatalog jobs will fail. Please set $HCAT_HOME to the root of your HCatalog installation. Warning: /opt/programs/sqoop-1.4.7.bin__hadoop-2.6.0/../accumulo does not exist! Accumulo imports will fail. Please set $ACCUMULO_HOME to the root of your Accumulo installation. Warning: /opt/programs/sqoop-1.4.7.bin__hadoop-2.6.0/../zookeeper does not exist! Accumulo imports will fail. Please set $ZOOKEEPER_HOME to the root of your Zookeeper installation. 23/06/10 16:18:23 INFO sqoop.Sqoop: Running Sqoop version: 1.4.7 23/06/10 16:18:23 WARN tool.BaseSqoopTool: Setting your password on the command-line is insecure. Consider using -P instead. 23/06/10 16:18:23 INFO manager.MySQLManager: Preparing to use a MySQL streaming resultset. 23/06/10 16:18:23 INFO tool.CodeGenTool: Beginning code generation Sat Jun 10 16:18:23 CST 2023 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. 23/06/10 16:18:24 INFO manager.SqlManager: Executing SQL statement: SELECT t.* FROM news AS t LIMIT 1 23/06/10 16:18:24 INFO manager.SqlManager: Executing SQL statement: SELECT t.* FROM news AS t LIMIT 1 23/06/10 16:18:24 INFO orm.CompilationManager: HADOOP_MAPRED_HOME is /opt/programs/hadoop-2.7.6 注: /tmp/sqoop-root/compile/84ba419f00fa83cb5d16dba722729d01/news.java使用或覆盖了已过时的 API。 注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。 23/06/10 16:18:25 INFO orm.CompilationManager: Writing jar file: /tmp/sqoop-root/compile/84ba419f00fa83cb5d16dba722729d01/news.jar 23/06/10 16:18:25 WARN manager.MySQLManager: It looks like you are importing from mysql. 23/06/10 16:18:25 WARN manager.MySQLManager: This transfer can be faster! Use the --direct 23/06/10 16:18:25 WARN manager.MySQLManager: option to exercise a MySQL-specific fast path. 23/06/10 16:18:25 INFO manager.MySQLManager: Setting zero DATETIME behavior to convertToNull (mysql) 23/06/10 16:18:25 ERROR tool.ImportTool: Import failed: No primary key could be found for table news. Please specify one with --split-by or perform a sequential import with '-m 1'.
最新发布
06-11

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值