morningman commented on a change in pull request #2685: add cpp connect sample
URL: https://github.com/apache/incubator-doris/pull/2685#discussion_r363759910
 
 

 ##########
 File path: samples/connect/cpp/my_doris.cpp
 ##########
 @@ -0,0 +1,156 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+/*************************************************************************
+> Useage:
+        1. g++ my_doris.cpp -o my_doris `mysql_config --cflags --libs`
+        2. ./my_doris
+> Comment: Supported mysql version: 5.6, 5.7, 8.0
+************************************************************************/
+
+#include <iostream>
+#include <string>
+#include "doris.h"
+
+using std::string;
+
+Doris::Doris()
+{
+    doris = mysql_init(NULL);   //init connection
+    if (doris == NULL)
+    {
+        std::cout << "Error:" << mysql_error(doris);
+    }
+}
+
+Doris::~Doris()
+{
+    if (doris != NULL)  //close connection
+    {
+        mysql_close(doris);
+    }
+}
+
+bool Doris::initDoris(const string& host, const string& user, const string& 
passwd,
+                      const string& db_name, int port, const string& sock)
+{
+    // create connection
+    doris = mysql_real_connect(doris, host.c_str(), user.c_str(), 
passwd.c_str(),
+            db_name.c_str(), port, sock.c_str(), 0);
+    if (doris == NULL)
+    {
+        std::cout << "Error: " << mysql_error(doris);
+        return false;
+    }
+    return true;
+}
+
+bool Doris::exeSQL(const string& sql)
+{
+    if (mysql_query(doris, sql.c_str()))
+    {
+        std::cout << "Query Error: " << mysql_error(doris);
+        return false;
+    }
+    else
+    {
+        result = mysql_store_result(doris);
+        if (result)
+        {
+           int num_fields = mysql_num_fields(result);
+           int num_rows = mysql_num_rows(result);
+           for (int i = 0; i < num_rows; i++)
+            {
+                row = mysql_fetch_row(result);
+                if (row < 0) break;
+
+                for (int j = 0; j < num_fields; j++)
+                {
+                    std::cout << row[j] << "\t";
+                }
+                std::cout << std::endl;
+            }
+
+        }
+        else
+        {
+            if (mysql_field_count(doris) == 0)
+            {
+                int num_rows = mysql_affected_rows(doris);
 
 Review comment:
   maybe better to print the `num_rows`?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to