<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sumit Asok on Medium]]></title>
        <description><![CDATA[Stories by Sumit Asok on Medium]]></description>
        <link>https://medium.com/@sumitasok?source=rss-6cc56c84588------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*3Q0Ivz5ukgXScp1c.png</url>
            <title>Stories by Sumit Asok on Medium</title>
            <link>https://medium.com/@sumitasok?source=rss-6cc56c84588------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 27 Nov 2026 10:23:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sumitasok/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[RAW SQL QUERY TO FETCH MYSQL TABLE ROW COUNT USING BEEGO ORM]]></title>
            <link>https://medium.com/@sumitasok/raw-sql-query-to-fetch-mysql-table-row-count-using-beego-orm-7d0f19c8ddb4?source=rss-6cc56c84588------2</link>
            <guid isPermaLink="false">https://medium.com/p/7d0f19c8ddb4</guid>
            <dc:creator><![CDATA[Sumit Asok]]></dc:creator>
            <pubDate>Thu, 26 Jun 2014 12:30:35 GMT</pubDate>
            <atom:updated>2014-06-26T12:32:26.260Z</atom:updated>
            <content:encoded><![CDATA[<h4>raw_sql_count_query_with_go_beego</h4><p>import package beego orm</p><pre>package main</pre><pre>import “github.com/astaxie/beego/orm”</pre><p>We have to initialize the mysql connection, for that we have to import the mysql dapter for golang</p><p>add `_ “github.com/go-sql-driver/mysql”` to the import statement</p><p>let us initialize the connection</p><pre>func init() {<br> orm.RegisterDriver(“mysql”, orm.DR_MySQL)<br> orm.RegisterDataBase(“default”, “mysql”, “mysql_username:mysql_password@/db_name?charset=utf8&quot;)<br>}</pre><p>We register the mysql Adapter here, and then Register Database</p><p>Couple of things to note,</p><p>1. We need to have a “default” connection,</p><p>2. database has to be specified again, here it is “mysql”</p><p>3. “mysql_username:mysql_password@/db_name” replace them with your credentials. You are running it on local machine and the charset is utf8 is my assumtion (just keeping the scope limited)</p><p>Lets say you have a Table called Visits with columns id, name</p><p>So we create a struct, that will act as the Model for the table</p><pre>type Visits struct {<br> Id int<br> Name string<br>}<br></pre><p>Now we have to register the Model to the ORM</p><p>in init function, we add `orm.RegisterModel(new(Visits))` after the existing lines of code.</p><p>Time to fetch some data,</p><p>create your main function</p><pre>function main() {<br> o := orm.NewOrm()<br> o.Using(“default”)<br>}</pre><p>you create an ORM instance, tell it which config to use. We have created a default one. This means you can define multiple databases</p><p>The query we want data from is</p><pre>select count(*) as Count from visits where name = ‘name’;</pre><p>and the return has to be an integer value, the count</p><p>lets create the call, and pass the value for name as a parameter, so we can show some runtime control over the query.</p><pre> var count int<br> o.Raw(“select count(*) as Count from visits where name = ?”, ‘plant’).QueryRow(&amp;count)</pre><p>add this to your main function and print the value, you will have got it!</p><p>your whole code look something like this</p><pre>package main</pre><pre>import (<br> “fmt”<br> “github.com/astaxie/beego/orm”<br> _ “github.com/go-sql-driver/mysql”<br>)</pre><pre>func init() {<br> orm.RegisterDriver(“mysql”, orm.DR_MySQL)<br> orm.RegisterDataBase(“default”, “mysql”, “mysql_username:mysql_password@/database_name?charset=utf8&quot;)</pre><pre> orm.RegisterModel(new(Visits))<br>}</pre><pre>type Visits struct {<br> Id int<br> Name string<br>}</pre><pre>func main() {<br> o := orm.NewOrm()<br> o.Using(“default”)</pre><pre> var count int<br> o.Raw(“select count(*) as Count from visits where name = ?”, “1&quot;).QueryRow(&amp;count)</pre><pre> fmt.Println(count)<br>}</pre><p>get the code here: <a href="https://gist.github.com/sumitasok/bfaa8a7194d3532878e9#file-orm_raw_query_integer-go">https://gist.github.com/sumitasok/bfaa8a7194d3532878e9#file-orm_raw_query_integer-go</a></p><p>couple of things, for using beego/orm you have to get it. Run go get github.com/astaxie/beego on your console</p><p>and to use println, import fmt package.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7d0f19c8ddb4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>