<?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 Harshit Mogalapalli on Medium]]></title>
        <description><![CDATA[Stories by Harshit Mogalapalli on Medium]]></description>
        <link>https://medium.com/@harshimogalapalli?source=rss-47de383205fc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Stories by Harshit Mogalapalli on Medium</title>
            <link>https://medium.com/@harshimogalapalli?source=rss-47de383205fc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 04:19:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@harshimogalapalli/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[Unlocking some systems stuff — Part 2]]></title>
            <link>https://medium.com/@harshimogalapalli/unlocking-some-systems-stuff-part-2-e9d5c41f23ba?source=rss-47de383205fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/e9d5c41f23ba</guid>
            <category><![CDATA[linux-kernel]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[low-level-language]]></category>
            <category><![CDATA[systems-programming]]></category>
            <dc:creator><![CDATA[Harshit Mogalapalli]]></dc:creator>
            <pubDate>Sun, 26 May 2024 04:37:44 GMT</pubDate>
            <atom:updated>2024-05-26T04:37:44.712Z</atom:updated>
            <content:encoded><![CDATA[<h3>Unlocking some systems stuff — Part 2</h3><p>For people who are interested in assembly language and who would want to see the assembly instructions along with the C code there is an excellent user-space utility called objdump to get assembly instructions for a C code.</p><p>This tool is part of the binutils GNU package.</p><p>Let us say you have this simple C program:</p><pre>// test.c<br><br>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br><br>int main(void)<br>{<br>        int num = 0;<br>        for (int i = 1; i &lt; 10; i++)<br>                printf(&quot;num is %d\n&quot;, num);<br>}</pre><p>Note that to include debug information, you should compile it like this:</p><pre>$ gcc -g -o test test.c</pre><p>Now that you have a binary, let us use objdump to collect assembly code for each line of C code that is present in test.c</p><pre>$ objdump -d --source ./test</pre><p>output of this looks something like this:</p><pre>0000000000401126 &lt;main&gt;:<br>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br><br>int main(void)<br>{<br>  401126: 55                    push   %rbp<br>  401127: 48 89 e5              mov    %rsp,%rbp<br>  40112a: 48 83 ec 10           sub    $0x10,%rsp<br> int num = 0;<br>  40112e: c7 45 f8 00 00 00 00  movl   $0x0,-0x8(%rbp)<br> for (int i = 1; i &lt; 10; i++)<br>  401135: c7 45 fc 01 00 00 00  movl   $0x1,-0x4(%rbp)<br>  40113c: eb 18                 jmp    401156 &lt;main+0x30&gt;<br>  printf(&quot;num is %d\n&quot;, num);<br>  40113e: 8b 45 f8              mov    -0x8(%rbp),%eax<br>  401141: 89 c6                 mov    %eax,%esi<br>  401143: bf 10 20 40 00        mov    $0x402010,%edi<br>  401148: b8 00 00 00 00        mov    $0x0,%eax<br>  40114d: e8 de fe ff ff        callq  401030 &lt;printf@plt&gt;<br> for (int i = 1; i &lt; 10; i++)<br>  401152: 83 45 fc 01           addl   $0x1,-0x4(%rbp)<br>  401156: 83 7d fc 09           cmpl   $0x9,-0x4(%rbp)<br>  40115a: 7e e2                 jle    40113e &lt;main+0x18&gt;<br>  40115c: b8 00 00 00 00        mov    $0x0,%eax<br>}<br>  401161: c9                    leaveq<br>  401162: c3                    retq</pre><p>That looks nice! I can view deeply into assembly in the level of registers on what’s being done internally. You can also use more options(-l for getting line numbers)</p><p>For people interested in looking at the source code of objdump,here it is as a part of GNU binutils package or repo:</p><p><a href="https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git">https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git</a></p><p>Please see objdump.c in bintuils/ directory.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e9d5c41f23ba" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unlocking some systems stuff — part 1]]></title>
            <link>https://medium.com/@harshimogalapalli/unlocking-some-systems-stuff-part-1-e6233eaadb8e?source=rss-47de383205fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/e6233eaadb8e</guid>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[systems-programming]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[kernel]]></category>
            <dc:creator><![CDATA[Harshit Mogalapalli]]></dc:creator>
            <pubDate>Sat, 25 May 2024 22:17:00 GMT</pubDate>
            <atom:updated>2024-05-25T22:25:21.764Z</atom:updated>
            <content:encoded><![CDATA[<p>Unlocking some systems stuff — part 1</p><p>I always wondered how we could continue typing in terminal with new lines and at a point let it know that I am done with giving inputs. Below are two simple ways to do that, of course the easier way is to use a file and then do it using input redirection( &lt; ) symbol.</p><p>Eg:</p><pre><br># way 1<br><br>$ sort<br>apple<br>mango<br>banana<br>// press a ctrl+d here<br><br># way 2<br><br>$ sort &lt;&lt;EOF<br>apple<br>mango<br>banana<br>EOF<br>// using a delimiter<br></pre><p>The second way is using heredoc in shell, the here document starts with &lt;&lt; followed by a delimiter to block input after seeing that.</p><p>Kernel code for this is in drivers/tty/n_tty.c which is mainly in n_tty_read and the exact place of handling eof is in copy_from_read_buf function.</p><p>Please use this repo for browsing through linux kernel code.</p><p><a href="https://github.com/torvalds/linux/blob/v6.9-rc7/drivers/tty/n_tty.c#L2203">https://github.com/torvalds/linux/blob/v6.9-rc7/drivers/tty/n_tty.c#L2203</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e6233eaadb8e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Connecting to MySQL-Part 2]]></title>
            <link>https://medium.com/@harshimogalapalli/connecting-to-mysql-part-2-9c00f9d7c107?source=rss-47de383205fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/9c00f9d7c107</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[windows]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Harshit Mogalapalli]]></dc:creator>
            <pubDate>Fri, 19 Jun 2020 06:51:23 GMT</pubDate>
            <atom:updated>2020-06-19T06:51:23.398Z</atom:updated>
            <content:encoded><![CDATA[<p>This story explains on how to connect to MySQL(the most popular Open Source DBMS) with different languages in Windows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SrUq1AFjOUMUgUlFJbkARw.png" /></figure><p><strong>Installing MySQL in Windows</strong></p><blockquote><a href="https://dev.mysql.com/downloads/installer/">Install MySQL</a></blockquote><h3>Connecting to MySQL with Python</h3><p>Prerequisites</p><blockquote><a href="https://www.anaconda.com/products/individual">Anaconda for python</a></blockquote><blockquote><a href="https://dev.mysql.com/downloads/installer/">MySQL</a></blockquote><p>To install connector/python open MySQL installer and add connector/python.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*sz74xP0YNdR9N_aG" /></figure><p>Connector for python is installed at C:\Program Files\MySQL\Connector Python 8.0</p><p>Open Anaconda prompt and type -</p><pre>jupyter notebook</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nzmVfI9lGBY6rKIn" /></figure><p>You will get a prompt open with select your browser and jupyter homepage is launched.</p><p>Create a new Python 3 file</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*T1uR6JUpH4oDRIK8" /></figure><pre>import mysql<br>import mysql.connector<br>x = mysql.connector.connect(host=&#39;host&#39;,port=3306,user=&#39;admin&#39;,password=&#39;password&#39;) <br>cursor = cnx.cursor()<br>cursor.execute(&quot;show databases&quot;)<br>for i in cursor:<br>    print(i)<br>cursor.close()<br>cnx.close()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gUGq1HK1GEUanOC2" /></figure><h3>Connecting to MySQL with C++</h3><p>Pre-requisites</p><blockquote><a href="https://visualstudio.microsoft.com/downloads/">Visual Studio 2019</a></blockquote><blockquote><a href="https://dev.mysql.com/downloads/installer/">MySQL</a></blockquote><blockquote><a href="https://www.boost.org/users/download/">Boost Libraries</a></blockquote><p>Other necessary installations</p><p>We require connector c++ to connect MySQL from c++ code. By default in Windows (x86, 32-bit), MSI Installer Connector c++8.0 is automatically installed at C:\Program Files\MySQL\Connector C++ 8.0</p><p>Even if connector c++ 8.0 installation is skipped during the start we can install/modify it at any time from MySQL installer — Community</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/983/0*R52nttESbK_oEG-_" /></figure><p>Unzip the boost files</p><p>We will also need to compile and work with c++ on Visual Studio so we need to install ‘Desktop Deployments with C++’ from Visual Studio installer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aGSrlB17g2Ev0Sj0" /></figure><p>After successfully installing all these we can begin with connecting MySQL from c++ code.</p><p>Open Visual Studio 2019 and create a new project and select ‘Empty Project’</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZED53gFjaIMfPbws" /></figure><p>Open ‘Solution Explorer’ window from View-&gt; Solution Explorer or using Ctrl+Alt+L</p><p>Use shortcut ctrl+shift+A to add a new item to source files.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/992/0*QGyQZtC46qFJJQqw" /></figure><p>Select C++ file</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*3WnRw9ePGFPBmF9G" /></figure><p>Right click the project in solution explorer and select properties</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/807/0*xiRVsrFcGKOqlQD2" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/0*xlO2wqzlOlqo4PsC" /></figure><p>Select Configuration Manager and select</p><p>Active solution configuration as ‘Release’</p><p>Active solution platform as ‘x64’</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/985/0*oZwJpWq7iz5sw691" /></figure><p>Select C/C++ from the left pane of properties.</p><p>Select and edit Additional include directories.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/0*19JZz_v1hdt9vMQG" /></figure><p>Add all these directories</p><p>C:\Program Files\MySQL\MySQL Server 8.0\lib</p><p>C:\{user}\Desktop\boost_1_66_0</p><p>C:\Program Files\MySQL\Connector C++ 8.0\include</p><p>C:\Program Files\MySQL\MySQL Server 8.0\include</p><p>Select Linker-&gt;General-&gt;Additional Library directories and add</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/974/0*-OxizZfXnP06GJX4" /></figure><p>C:\Program Files\MySQL\Connector C++ 8.0\lib64\vs14</p><p>C:\Program Files\MySQL\Connector C++ 8.0\lib64</p><p>Select linker-&gt;input-&gt; Additional Depedencies edit and add</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/972/0*0mCMjPJhJp7u-VTZ" /></figure><pre>mysqlcppconn.lib</pre><pre>libmysql.lib</pre><pre>libmysql.dll</pre><p>Sometimes even though we have included all the paths to dll/lib still while build error can arise that these xyz.dll was missing. In this case just go to MySQL folder in Program Files and search for this DLL/lib and add it to your Visual Studio Project workspace directory.(right click project in solution explorer and select open folder in file explorer).</p><p>In my case I had to copy these dll/lib files</p><pre>libmysql.lib</pre><pre>mysqlcppconn7-vs-14.dll</pre><p>We have added most of the paths and dependencies and we can compile our code now.</p><pre>#include &lt;stdlib.h&gt;<br>#include &lt;iostream&gt;<br></pre><pre>#include &lt;mysql/jdbc.h&gt;</pre><pre>using namespace std;</pre><pre>int main(void)<br>{<br>    sql::Driver* driver;<br>    sql::Connection* con;<br>    sql::Statement* stmt;<br>    sql::ResultSet* res;<br>    driver = get_driver_instance();<br>    con = driver-&gt;connect(&quot;host&quot;, &quot;usert&quot;, &quot;password&quot;);<br>    con-&gt;setSchema(&quot;testdb&quot;); //select any pre-existing schema<br>    stmt = con-&gt;createStatement();<br>    res = stmt-&gt;executeQuery(&quot;SELECT * from x&quot;); //select all values from table x in testdb<br>    while (res-&gt;next()) {<br>        cout &lt;&lt; res-&gt;getString(1) &lt;&lt; endl;<br>    }<br>    delete res;<br>    delete stmt;<br>    delete con;<br>}</pre><p>Build this code using ctrl+shift+B. Done :)</p><h3>Connecting to MySQL with C</h3><p>(continuation after c++)</p><p>Follow all the steps in c++ except making source.cpp file</p><p>Add a source file but make sure you change the extension in name from cpp to c</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kzNGSZRuTToDYjDo" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BXrfvIKMoc7aIkqc" /></figure><p>(Note you must change the extension from cpp to c)</p><pre>#include&lt;mysql.h&gt;<br>#include&lt;stdio.h&gt;<br>int main() {<br>    MYSQL* conn;<br>    MYSQL_RES* res;<br>    MYSQL_ROW row;<br>    char* server = &quot;host&quot;;<br>    char* user = &quot;user&quot;;<br>    char* password = &quot;password&quot;;<br>    char* database = &quot;database&quot;;</pre><pre>    conn = mysql_init(NULL);<br>    printf(&quot;Connecting\n&quot;);<br>    <br>    if (!mysql_real_connect(conn, server,<br>        user, password, database, 0, NULL, 0)) {<br>        fprintf(stderr, &quot;%s\n&quot;, mysql_error(conn));<br>        return(0);<br>    }<br>    printf(&quot;Client version: %s\n&quot;, mysql_get_client_info());<br>    <br>    if (mysql_query(conn, &quot;show databases;&quot;)) {<br>        fprintf(stderr, &quot;%s\n&quot;, mysql_error(conn));<br>        return(0);<br>    }<br>    res = mysql_use_result(conn);<br>   <br>   /<br>    while ((row = mysql_fetch_row(res)) != NULL)<br>        printf(&quot;%s\n&quot;, row[0]);<br>  <br>    mysql_free_result(res);<br>    mysql_close(conn);<br>}</pre><p>Build the solution using ctrl + F5 or from top Build-&gt;Build solution</p><p>Note — if any of the dll files are missing just copy them from C:\Program Files\MySQL and paste them to the workspace directory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mGTAyEPYt2sDwGfg" /></figure><h3>Connecting to MySQL with Java</h3><p>Pre-requisites</p><p><a href="https://www.oracle.com/java/technologies/javase-jdk14-downloads.html">JDK</a></p><p><a href="https://dev.mysql.com/downloads/installer/">MySQL</a></p><p>To install connector/python open MySQL installer and add connector J.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/0*prkX97UOEG9ML_0Q" /></figure><p>Connector J is installed at C:\Program Files (x86)\MySQL\Connector J 8.0</p><p>After installing java edit the environment variables and add jdk bin location C:\Program Files\Java\jdk-14.0.1\bin to PATH</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/0*Y2Gph_jCXJYopwi0" /></figure><p>Open cmd and check the java version to check if java is properly installed and can be used from cmd.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*jZp0OF-jdoOqiFS4" /></figure><p>If java is installed you are ready to go.</p><p>Open any text editor and write this code and save it as ConnectionCheck.java</p><pre>import java.sql.*;  <br>class ConnectionCheck{  <br>public static void main(String args[]){  <br>try{  <br>Class.forName(&quot;com.mysql.cj.jdbc.Driver&quot;);  // for connector J 5.1 use com.mysql.jdbc.Driver</pre><pre>Connection con=DriverManager.getConnection(  <br>&quot;jdbc:mysql://localhost&quot;,&quot;root&quot;,&quot;password-mysql&quot;); </pre><pre>System.out.println(con);</pre><pre>Statement stmt=con.createStatement();  <br>stmt.executeUpdate(&quot;create database newdb&quot;);  <br>System.out.println(&quot;Done&quot;);</pre><pre>con.close();  <br>}catch(Exception e){ System.out.println(e);}  <br>}  <br>}</pre><p>Open connector J by going to C:\Program Files (x86)\MySQL\Connector J 8.0 and note the name of the connector jar file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/887/0*1mthfcRMC-2ZlY3i" /></figure><p>We need to set the -classpath flag to this file. So make a note of it for in this case the classpath is “C:\Program Files (x86)\MySQL\Connector J 8.0\mysql-connector-java-8.0.20.jar”</p><p>We are all set to compile our code.</p><p>Open cmd and go to the directory where you saved the code</p><p>Compile using this command -</p><pre>javac -classpath “C:\Program Files (x86)\MySQL\Connector J 8.0\mysql-connector-java-8.0.20.jar”; ConnectionCheck.java</pre><p>To run the compiled file use -</p><pre>java -classpath “C:\Program Files (x86)\MySQL\Connector J 8.0\mysql-connector-java-8.0.20.jar”; ConnectionCheck</pre><h3>Connecting to MySQL with Nodejs</h3><p>Create a database named testdb and create a table and insert 2 values.</p><p>Connect to mysql</p><pre>mysql -u root -p</pre><p>enter your mysql password;</p><pre>create database dbtest;</pre><pre>use dbtest;</pre><pre>create table t(id int primary key,name text);</pre><pre>insert into t(1,”harshit”);</pre><pre>insert into t(2,”abhishek”);</pre><p>Check whether the tables are populated properly.</p><pre>mysql&gt; select * from t;</pre><p>outputshould be this.</p><pre>+ — — + — — — — — +</pre><pre>| id | name       |</pre><pre>+ — — + — — — — — +</pre><pre>| 1 | harshit     |</pre><pre>| 2 | abhishek    |</pre><pre>+ — — + — — — — — +</pre><pre>2 rows in set (0.00 sec)</pre><p>now you are all set.</p><p>After installing MySQL</p><p>Install mysql driver with npm</p><pre>C:\Users\Harshit&gt;npm install mysql</pre><p>Go to the path of node modules and make a empty document and copy this code</p><p>CODE-</p><pre>var mysql = require(‘mysql’);</pre><pre>var connection = mysql.createConnection({</pre><pre>host: ‘localhost’,</pre><pre>user: ‘root’,</pre><pre>password: ‘your-password’,</pre><pre>database: ‘dbtest’</pre><pre>});</pre><pre>connection.connect(function(e) {</pre><pre>if (e) {</pre><pre>return console.error(‘error: ‘ + e.message);</pre><pre>}</pre><pre>console.log(‘\nConnected to the MySQL server…\n’);</pre><pre>});</pre><pre>$query = ‘SELECT * from t’;</pre><pre>connection.query($query, function(e, rows) {</pre><pre>if(e){</pre><pre>console.log(“Error ocurred in executing the query.”);</pre><pre>return;</pre><pre>}</pre><pre>console.log(“The records of table are:\n”);</pre><pre>for(var row of rows) {</pre><pre>console.log(row[‘id’],row[‘name’]);</pre><pre>}</pre><pre>});</pre><pre>connection.end(function(){</pre><pre>console.log(‘\nConnection closed.\n’);</pre><pre>});</pre><p>Save the code as server.js</p><p>Run it as</p><pre>node server.js</pre><p>Your output should be this in the terminal</p><p>Connected to the MySQL server…</p><p>The records of table are:</p><p>1 ‘harshit’</p><p>2 ‘abhishek’</p><p>Connection closed.</p><h3>Connecting to MySQL with .NET</h3><p>Requirements:-</p><ol><li>Install .NET</li></ol><p><a href="https://dotnet.microsoft.com/download">Install</a></p><p>Installing .NET on Windows is intuitive and can be done by running the downloaded .exe file.</p><p>2. Install Visual Studio 2019.</p><p>Now in a new cmd window run the following commands:-</p><pre>mkdir MySqlExample</pre><pre>cd MySqlExample</pre><pre>dotnet new console</pre><pre>dotnet add package MySqlConnector</pre><p>These commands will generate the following files in the directory:-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/766/0*y_E8o3iuIK6u5rQx" /></figure><p>We will work on Program.cs</p><p>Paste the following code in Program.cs</p><pre>using System;</pre><pre>using System.Threading.Tasks;</pre><pre>using MySql.Data.MySqlClient;</pre><pre>namespace MySqlExample</pre><pre>{</pre><pre>class MySqlCreate</pre><pre>{</pre><pre>static async Task Main(string[] args)</pre><pre>{</pre><pre>var builder = new MySqlConnectionStringBuilder</pre><pre>{</pre><pre>Server = &quot;&lt;host address&gt;&quot;,</pre><pre>Database = &quot;test&quot;,</pre><pre>UserID = &quot;&lt;youruserid&gt;&quot;,</pre><pre>Password = &quot;StrongPassword!&quot;,</pre><pre>//only if ssl is enabled in the instance</pre><pre>SslMode = MySqlSslMode.Required,</pre><pre>};</pre><pre>using (var conn = new MySqlConnection(builder.ConnectionString))</pre><pre>{</pre><pre>Console.WriteLine(&quot;Opening connection&quot;);</pre><pre>await conn.OpenAsync();</pre><pre>using (var command = conn.CreateCommand())</pre><pre>{</pre><pre>command.CommandText = &quot;DROP TABLE IF EXISTS inventory;&quot;;</pre><pre>await command.ExecuteNonQueryAsync();</pre><pre>Console.WriteLine(&quot;Finished dropping table (if existed)&quot;);</pre><pre>command.CommandText = &quot;CREATE TABLE inventory (id serial PRIMARY KEY, name VARCHAR(50), quantity INTEGER);&quot;;</pre><pre>await command.ExecuteNonQueryAsync();</pre><pre>Console.WriteLine(&quot;Finished creating table&quot;);</pre><pre>command.CommandText = @&quot;INSERT INTO inventory (name, quantity) VALUES (@name1, @quantity1),</pre><pre>(@name2, @quantity2), (@name3, @quantity3);&quot;;</pre><pre>command.Parameters.AddWithValue(&quot;@name1&quot;, &quot;banana&quot;);</pre><pre>command.Parameters.AddWithValue(&quot;@quantity1&quot;, 150);</pre><pre>command.Parameters.AddWithValue(&quot;@name2&quot;, &quot;orange&quot;);</pre><pre>command.Parameters.AddWithValue(&quot;@quantity2&quot;, 154);</pre><pre>command.Parameters.AddWithValue(&quot;@name3&quot;, &quot;apple&quot;);</pre><pre>command.Parameters.AddWithValue(&quot;@quantity3&quot;, 100);</pre><pre>int rowCount = await command.ExecuteNonQueryAsync();</pre><pre>Console.WriteLine(String.Format(&quot;Number of rows inserted={0}&quot;, rowCount));}</pre><pre>// connection will be closed by the &#39;using&#39; block</pre><pre>Console.WriteLine(&quot;Closing connection&quot;);}</pre><pre>Console.WriteLine(&quot;Press RETURN to exit&quot;);</pre><pre>Console.ReadLine();</pre><pre>}}}</pre><p>Now we can run the program using:-</p><p>dotnet run — project MysqlExample.csproj</p><p>The output will be:-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/786/0*q_mbvrDfRVO9fbQ3" /></figure><h3>Authors:</h3><pre>Abhishek Dhanwani(abhishek.dhanwani@oracle.com) ,</pre><pre>Anupreet Singh(anupreet.singh@oracle.com),</pre><pre>Harshit Mogalapalli(harshit.mogalapalli@oracle.com).</pre><h3>Disclaimer:</h3><p>We are interning with Oracle and all views and information shared is personal. The information and views expressed may not be construed as the official Oracle view.</p><h3>Hope this story on Connecting to MySQL helped you.</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9c00f9d7c107" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Connecting to MySQL-Part 1]]></title>
            <link>https://medium.com/@harshimogalapalli/connecting-to-mysql-part-1-4d49e5b7c5de?source=rss-47de383205fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/4d49e5b7c5de</guid>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[ubuntu]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Harshit Mogalapalli]]></dc:creator>
            <pubDate>Fri, 19 Jun 2020 06:47:54 GMT</pubDate>
            <atom:updated>2020-06-19T06:47:54.099Z</atom:updated>
            <content:encoded><![CDATA[<p>This story explains on how to connect to MySQL(the most popular Open Source DBMS) with different languages in Ubuntu.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SrUq1AFjOUMUgUlFJbkARw.png" /></figure><p><strong>Installing MySQL in Ubuntu</strong></p><pre>sudo apt-get update</pre><pre>sudo apt-get install mysql-server</pre><h3><strong>Connecting to MySQL with Python</strong></h3><p>After installing MySQL install python3 and pip .</p><p>Install python and pip.</p><pre>sudo apt install python3</pre><pre>sudo apt install python3-pip</pre><p>Install the mysql connector via pip3</p><pre>pip3 install mysql-connector</pre><p>Create python file</p><pre>touch server.py</pre><p>Insert this code to see all the databases after connection.</p><pre>nano server.py</pre><p>Code:</p><pre>import mysql.connector<br><br>mydb = mysql.connector.connect(<br>  host=&quot;localhost&quot;,<br>  user=&quot;root&quot;,<br>  passwd=&quot;harshit&quot;<br>)<br><br><br>mycursor = mydb.cursor()<br><br>mycursor.execute(&quot;SHOW DATABASES&quot;)<br><br>for x in mycursor:<br>  print(x)</pre><p>Ctrl+O to Save and press Enter and then Ctrl+X to exit</p><p>Execute python code</p><pre>python3 server.py</pre><p>Output is all the databases</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/675/0*TBnJYkhtDA1c21sH" /></figure><p>Successfully done.</p><h3><strong>Connecting to MySQL with C++</strong></h3><p>After installing MySQL we have to install c compiler gcc</p><pre>sudo apt install g++</pre><pre>sudo apt install gcc</pre><p>Install the connector of C++ with MySQL</p><pre>sudo apt-get install libmysqlcppconn-dev</pre><p>we are doing this to show all contents of a database named testdb</p><p>if you dont have it please create it .</p><pre>mysql -u root -p</pre><p>enter your mysql password;</p><pre>create database dbtest;</pre><pre>use dbtest;</pre><pre>create table t(id int primary key,name text);</pre><pre>insert into t(1,”harshit”);</pre><pre>insert into t(2,”abhishek”);</pre><p>Check whether the tables are populated properly.</p><pre>mysql&gt; select * from t;</pre><p>output should be the entries harshit and abhishek.</p><p>now you are all set.</p><p>Make a cpp file</p><pre>nano server.cpp</pre><p>Copy this code</p><pre>#include &lt;stdlib.h&gt;<br>#include &lt;iostream&gt;<br>#include &lt;sstream&gt;<br>#include &lt;stdexcept&gt;<br>#include &quot;mysql_connection.h&quot;<br>#include &lt;cppconn/driver.h&gt;<br>#include &lt;cppconn/exception.h&gt;<br>#include &lt;cppconn/resultset.h&gt;<br>#include &lt;cppconn/statement.h&gt;<br>#include &lt;cppconn/prepared_statement.h&gt;<br>#define EXAMPLE_HOST &quot;localhost&quot;<br>#define EXAMPLE_USER &quot;root&quot;<br>#define EXAMPLE_PASS &quot;password&quot;<br>#define EXAMPLE_DB &quot;dbtest&quot;<br>using namespace std;<br>int main(int argc, const char **argv)<br>{<br>     const string url=EXAMPLE_HOST;<br>     const string user=EXAMPLE_USER;<br>     const string pass=EXAMPLE_PASS;<br>     const string database=EXAMPLE_DB;<br>     sql::Driver* driver = get_driver_instance();<br>sql::Statement* stmt;<br>sql::ResultSet* res;<br><br>     std::auto_ptr&lt;sql::Connection&gt; con(driver-&gt;connect(url, user, pass));<br>     con-&gt;setSchema(database);<br> stmt = con-&gt;createStatement();<br>res = stmt-&gt;executeQuery(&quot;SELECT * from t&quot;); //select all values from table$<br>while (res-&gt;next()) {<br>     cout &lt;&lt; res-&gt;getString(1)&lt;&lt;res-&gt;getString(2) &lt;&lt; endl;<br>}<br>delete res;<br>delete stmt;<br>    <br>     return 0;<br>}</pre><p>Ctrl+O to Save and press Enter and then Ctrl+X to exit</p><p>Compile the file</p><pre>sudo g++ -Wall -I/usr/include/cppconn -o testapp server.cpp -L/usr/lib -lmysqlcppconn</pre><p>Run the .sh file</p><pre>./testapp</pre><p>Output should be like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/497/0*AJXiePyGUQbF4zvV" /></figure><h3><strong>Connecting to MySQL with C</strong></h3><p>After installing MySQL we have to install c compiler gcc</p><pre>sudo apt install gcc</pre><p>Install Connector and env</p><pre>sudo apt install libmysqlclient-dev</pre><pre>sudo apt-get install libmysqlcppconn-dev</pre><p>Write the C Code</p><pre>nano s.c</pre><p>Here is the Code</p><pre>#include&lt;stdio.h&gt;<br>#include&lt;stdlib.h&gt;<br>#include&lt;mysql/mysql.h&gt;<br>static char *host=&quot;localhost&quot;;<br>static char *user=&quot;root&quot;;<br>static char *pass=&quot;harshit&quot;;<br>static char *dbname=&quot;testdb_c11&quot;;<br>unsigned int port =3306;<br>static char *unix_socket=NULL;<br>unsigned int flag=0;<br>const char *q=&quot;show databases;&quot;;<br>int main(){<br>MYSQL *conn;<br>conn=mysql_init(NULL);<br>if(!(mysql_real_connect(conn,host,user,pass,dbname,port,unix_socket,flag))){<br>fprintf(stderr,&quot;\nError: %s [%d]\n&quot;,mysql_error(conn),mysql_error(conn));<br>exit(1);<br>}<br>printf(&quot;Connection Successful\n&quot;);<br>if (mysql_query(conn, &quot;CREATE DATABASE testdb_c11&quot;))<br>{<br>fprintf(stderr, &quot;%s\n&quot;, mysql_error(conn));<br>mysql_close(conn);<br>exit(1);<br>}<br>return EXIT_SUCCESS;<br>}</pre><p>Compile the C file</p><pre>gcc -o outconn $(mysql_config — cflags) s.c $(mysql_config — libs)</pre><p>Run the file</p><pre>./outconn</pre><p>See the output</p><p>In the mysql client check databases.</p><blockquote>mysql&gt; show databases;</blockquote><p>Output should have testdb_c11.</p><h3><strong>Connecting to MySQL with Java</strong></h3><p>Here in java the connection to database is made and we are creating a database named db_java.</p><p>After installing MySQL from above commands on the top of the page check whether java is installed don’t worry if not.</p><p>Install jre</p><pre>sudo apt-get install default-jre</pre><p>Install jdk</p><pre>sudo apt-get install default-jdk</pre><p>Check java version</p><pre>java -version</pre><p>output should be like this</p><p>openjdk version “1.8.0_252”</p><p>OpenJDK Runtime Environment (build 1.8.0_252–8u252-b09–1~16.04-b09)</p><p>OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)</p><p>Make a java file using gedit or touch</p><pre>touch db.java</pre><p>Insert the code</p><pre>nano db.java</pre><p>Copy paste the code</p><pre>import java.sql.*;   <br>class simple{   <br>public static void main(String args[]){   <br>try{   <br>Class.forName(&quot;com.mysql.jdbc.Driver&quot;);   <br> <br>Connection con=DriverManager.getConnection(   <br>&quot;jdbc:mysql://localhost&quot;,&quot;root&quot;,&quot;password-mysql&quot;);  <br> <br>    System.out.println(con);<br> <br>//here sonoo is database name, root is username and password   <br>Statement stmt=con.createStatement();   <br>stmt.executeUpdate(&quot;create database db_java&quot;);   <br> <br>con.close();   <br>}catch(Exception e){ System.out.println(e);}   <br>}   <br>}</pre><p>End of code</p><p>We have to install java mysql driver</p><pre>sudo apt-get install libmysql-java</pre><p>Set the path</p><pre>export CLASSPATH = $CLASSPATH:/usr/share/java/mysql-connector-java.jar</pre><p>Now you are ready to go with java program.</p><pre>javac db.java</pre><p>Execute the code</p><pre>java db</pre><p>Warnings may be shown but you are properly connected.</p><p>Check your mysqll database</p><pre>mysql -u root -p</pre><p>password</p><pre>show databases;</pre><p>You should see the our created database in the output.</p><h3><strong>Connecting to MySQL with Nodejs</strong></h3><p>Create a database named testdb and create a table and insert 2 values.</p><p>Connect to mysql</p><pre>mysql -u root -p</pre><p>enter your mysql password;</p><pre>create database dbtest;</pre><pre>use dbtest;</pre><pre>create table t(id int primary key,name text);</pre><pre>insert into t(1,”harshit”);</pre><pre>insert into t(2,”abhishek”);</pre><p>Check whether the tables are populated properly.</p><pre>mysql&gt; select * from t;</pre><p>outputshould be this.</p><pre>+ — — + — — — — — +</pre><pre>| id | name       |</pre><pre>+ — — + — — — — — +</pre><pre>| 1 | harshit     |</pre><pre>| 2 | abhishek    |</pre><pre>+ — — + — — — — — +</pre><pre>2 rows in set (0.00 sec)</pre><p>now you are all set.</p><p>After installing MySQL install package manager.</p><pre>sudo apt-get install npm</pre><p>Install MySQL driver for nodejs</p><pre>sudo npm install mysql</pre><p>Make a directory</p><pre>mkdir node-mysql</pre><pre>touch server.js</pre><pre>nano server.js</pre><p>CODE-</p><pre>var mysql = require(‘mysql’);</pre><pre>var connection = mysql.createConnection({</pre><pre>host: ‘localhost’,</pre><pre>user: ‘root’,</pre><pre>password: ‘your-password’,</pre><pre>database: ‘dbtest’</pre><pre>});</pre><pre>connection.connect(function(e) {</pre><pre>if (e) {</pre><pre>return console.error(‘error: ‘ + e.message);</pre><pre>}</pre><pre>console.log(‘\nConnected to the MySQL server…\n’);</pre><pre>});</pre><pre>$query = ‘SELECT * from t’;</pre><pre>connection.query($query, function(e, rows) {</pre><pre>if(e){</pre><pre>console.log(“Error ocurred in executing the query.”);</pre><pre>return;</pre><pre>}</pre><pre>console.log(“The records of table are:\n”);</pre><pre>for(var row of rows) {</pre><pre>console.log(row[‘id’],row[‘name’]);</pre><pre>}</pre><pre>});</pre><pre>connection.end(function(){</pre><pre>console.log(‘\nConnection closed.\n’);</pre><pre>});</pre><p>Ctrl+O to Save and Ctrl+X to save</p><p>Run the code</p><pre>node server.js</pre><p>Your output should be this in the terminal</p><p>Connected to the MySQL server…</p><p>The records of table are:</p><p>1 ‘harshit’</p><p>2 ‘abhishek’</p><p>Connection closed.</p><h3>Connecting to MySQL with .NET</h3><p>To install dotnet on Ubuntu, check if your current version supports dotnet or not using <a href="https://docs.microsoft.com/en-us/dotnet/core/install/linux-ubuntu#">Install</a></p><p>To install dotnet, run the following commands in order:-</p><pre>wget <a href="https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb">https://packages.microsoft.com/config/ubuntu/16.04/packages-microsoft-prod.deb</a> -O packages-microsoft-prod.deb</pre><pre>sudo dpkg -i packages-microsoft-prod.deb</pre><pre>sudo apt-get update</pre><pre>sudo apt-get install -y apt-transport-https</pre><pre>sudo apt-get update</pre><pre>sudo apt-get install -y dotnet-sdk-3.1</pre><p>The rest of the codes are exactly the same as in Windows.</p><p>Create a dotnet project using the following commands:-</p><pre>mkdir MySqlExample</pre><pre>cd MySqlExample</pre><pre>dotnet new console</pre><pre>dotnet add package MySqlConnector</pre><p>After this the directories are created in the manner shown</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/965/1*4Cy_6pVVcRy78DNGWwcfzg.png" /></figure><p>After this paste the same code as above in the Program.cs file</p><p>To run the project use:-</p><pre>dotnet run — project MySqlExample.csproj</pre><p>The output is shown:-</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/851/1*MMiT9fpdWsgXttgqI7oxCA.png" /></figure><h3>Authors:</h3><pre>Abhishek Dhanwani(abhishek.dhanwani@oracle.com) , </pre><pre>Anupreet Singh(anupreet.singh@oracle.com), </pre><pre>Harshit Mogalapalli(harshit.mogalapalli@oracle.com).</pre><h3>Disclaimer:</h3><p>We are interning with Oracle and all views and information shared is personal. The information and views expressed may not be construed as the official Oracle view.</p><h3><strong>Hope this story on Connecting to MySQL helped you.</strong></h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4d49e5b7c5de" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>