www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - mysql-native Help required

reply Vino <akashvino79 gmail.com> writes:
Hi All,


   Request your help on the below code as it is not working as 
expected.

GetConnections.d

module common.GetConnections;
import mysql;

class Connections
{
   private Connection conn;
   auto constr = 

   this.conn = new Connection(constr);
}

GetHost.d

import common.GetConnections;
import std.array : array;
import std.variant;
import mysql;
import std.stdio: writeln;

void main()
{
  auto conn = new Connections();
  Row[] data = conn.query("SELECT * FROM hostlog").array;
  writeln(data[]);
}

From,
Vino
Oct 22 2020
next sibling parent novice3 <sorryno em.ail> writes:
On Thursday, 22 October 2020 at 11:04:53 UTC, Vino wrote:
 class Connections
 {
   private Connection conn;
   auto constr = 

   this.conn = new Connection(constr);
 }
where Connections class constructor implemented?
Oct 22 2020
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 10/22/20 7:04 AM, Vino wrote:
 Hi All,
 
 
    Request your help on the below code as it is not working as expected.
 
 GetConnections.d
 
 module common.GetConnections;
 import mysql;
 
 class Connections
 {
    private Connection conn;
    auto constr = 

I'd declare this immutable, so it's not stored in the class: immutable constr = "...";
    this.conn = new Connection(constr);
You are trying to assign an instance member at compile time, with no actual instance. You need a constructor this() { this.conn = new Connection(constr); }
 }
 
 GetHost.d
 
 import common.GetConnections;
 import std.array : array;
 import std.variant;
 import mysql;
 import std.stdio: writeln;
 
 void main()
 {
   auto conn = new Connections();
   Row[] data = conn.query("SELECT * FROM hostlog").array;
   writeln(data[]);
 }
The rest should work. -Steve
Oct 22 2020
parent reply Vino <akashvino79 gmail.com> writes:
On Thursday, 22 October 2020 at 14:08:30 UTC, Steven 
Schveighoffer wrote:
 On 10/22/20 7:04 AM, Vino wrote:
 Hi All,
 
 
    Request your help on the below code as it is not working as 
 expected.
 
 GetConnections.d
 
 module common.GetConnections;
 import mysql;
 
 class Connections
 {
    private Connection conn;
    auto constr = 

I'd declare this immutable, so it's not stored in the class: immutable constr = "...";
    this.conn = new Connection(constr);
You are trying to assign an instance member at compile time, with no actual instance. You need a constructor this() { this.conn = new Connection(constr); }
 }
 
 GetHost.d
 
 import common.GetConnections;
 import std.array : array;
 import std.variant;
 import mysql;
 import std.stdio: writeln;
 
 void main()
 {
   auto conn = new Connections();
   Row[] data = conn.query("SELECT * FROM hostlog").array;
   writeln(data[]);
 }
The rest should work. -Steve
Hi Steve, Thank you very much, I tried your solution, still not working File: GetConnections.d module common.GetConnections; import mysql; class Connections { private Connection conn; immutable constr = this() { this.conn = new Connection(constr); } } Error: source/common/GetHost.d(11,25): Error: none of the overloads of query are callable using argument types (Connections, string), candidates are: /root/.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql commands.d(321,13): mysql.commands.query(Connection conn, const(char[]) sql, ColumnSpecialization[] csa = null) /root/.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql commands.d(334,13): mysql.commands.query(Connection conn, const(char[]) sql, VariantN!32LU[] args) /root/.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql commands.d(342,13): mysql.commands.query(Connection conn, ref Prepared prepared) /root/.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql commands.d(357,13): mysql.commands.query(Connection conn, ref Prepared prepared, VariantN!32LU[] args) /root/.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql commands.d(364,13): mysql.commands.query(Connection conn, ref BackwardCompatPrepared prepared) source/common/GetParams.d(11,25): ... (2 more, -v to show) ... From, Vino.B
Oct 22 2020
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 10/22/20 11:00 AM, Vino wrote:
 On Thursday, 22 October 2020 at 14:08:30 UTC, Steven Schveighoffer wrote:
 On 10/22/20 7:04 AM, Vino wrote:
 Hi All,


    Request your help on the below code as it is not working as expected.

 GetConnections.d

 module common.GetConnections;
 import mysql;

 class Connections
 {
    private Connection conn;
    auto constr = 

I'd declare this immutable, so it's not stored in the class: immutable constr = "...";
    this.conn = new Connection(constr);
You are trying to assign an instance member at compile time, with no actual instance. You need a constructor      this() { this.conn = new Connection(constr); }
 }

 GetHost.d

 import common.GetConnections;
 import std.array : array;
 import std.variant;
 import mysql;
 import std.stdio: writeln;

 void main()
 {
   auto conn = new Connections();
   Row[] data = conn.query("SELECT * FROM hostlog").array;
   writeln(data[]);
 }
The rest should work. -Steve
Hi Steve,    Thank you very much, I tried your solution, still not working File: GetConnections.d module common.GetConnections; import mysql; class Connections {   private Connection conn;   immutable constr =   this() { this.conn = new Connection(constr); } } Error: source/common/GetHost.d(11,25): Error: none of the overloads of query are callable using argument types (Connections, string), candidates are: /root/.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql commands.d(321,13): mysql.commands.query(Connection conn, const(char[]) sql, ColumnSpecialization[] csa = null) /root/.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql commands.d(334,13): mysql.commands.query(Connection conn, const(char[]) sql, VariantN!32LU[] args) /root/.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql commands.d(342,13): mysql.commands.query(Connection conn, ref Prepared prepared) /root/.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql commands.d(357,13): mysql.commands.query(Connection conn, ref Prepared prepared, VariantN!32LU[] args) /root/.dub/packages/mysql-native-3.0.0/mysql-native/source/mysql commands.d(364,13): mysql.commands.query(Connection conn, ref BackwardCompatPrepared prepared) source/common/GetParams.d(11,25):        ... (2 more, -v to show) ....
Different error: Row[] data = conn.query("SELECT * FROM hostlog").array; This is trying to call mysql-native's UFCS query function on Connections, which isn't valid. You need to call it on conn.conn. But there's no access to the private Connection conn inside the Connections class. I'm not sure what the class is for anyway, so it's hard for me to suggest a proper alternative. Are you just trying to encapsulate the connection string? I'd suggest instead of a whole class, just a factory function: Connection getConnection() { return new Connection("..."); } -Steve
Oct 22 2020
parent reply aberba <karabutaworld gmail.com> writes:
On Thursday, 22 October 2020 at 18:43:40 UTC, Steven 
Schveighoffer wrote:
 On 10/22/20 11:00 AM, Vino wrote:
 [...]
Different error: Row[] data = conn.query("SELECT * FROM hostlog").array; This is trying to call mysql-native's UFCS query function on Connections, which isn't valid. You need to call it on conn.conn. But there's no access to the private Connection conn inside the Connections class. I'm not sure what the class is for anyway, so it's hard for me to suggest a proper alternative. Are you just trying to encapsulate the connection string? I'd suggest instead of a whole class, just a factory function: Connection getConnection() { return new Connection("..."); } -Steve
Was about to say that. Part of why I think some people hate OOP...due to misuse. All my MySQL projects have this getConnection() function.
Oct 23 2020
parent Vino <akashvino79 gmail.com> writes:
On Friday, 23 October 2020 at 20:28:40 UTC, aberba wrote:
 On Thursday, 22 October 2020 at 18:43:40 UTC, Steven 
 Schveighoffer wrote:
 [...]
Was about to say that. Part of why I think some people hate OOP...due to misuse. All my MySQL projects have this getConnection() function.
Hi All, Thank you very much, it resolved my issues. Vino.B
Oct 24 2020