function to create tables for Access and MySQL

function to create tables for Access and MySQL

Postby lucasdebeltran » Sat Jun 08, 2013 5:52 pm

Hello,

As the sintax is different, is there a function to créate tables for either Access or MySQL?.

Thank you.
Muchas gracias. Many thanks.

Un saludo, Best regards,

Harbour 3.2.0dev, Borland C++ 5.82 y FWH 13.06 [producción]

Implementando MSVC 2010, FWH64 y ADO.

Abandonando uso xHarbour y SQLRDD.
User avatar
lucasdebeltran
 
Posts: 1303
Joined: Tue Jul 21, 2009 8:12 am

Re: function to create tables for Access and MySQL

Postby Enrico Maria Giordano » Sat Jun 08, 2013 7:37 pm

Lucas,

lucasdebeltran wrote:Hello,

As the sintax is different, is there a function to créate tables for either Access or MySQL?.

Thank you.


This is the function I'm using:

Code: Select all  Expand view
STATIC FUNCTION ADDTABLE( cMot, cTab, aFld )

    LOCAL cQuery := "CREATE TABLE " + cTab + " ( "

    LOCAL cType

    LOCAL i

    IF cMot == "JET"
        cQuery += "Id COUNTER PRIMARY KEY, "
    ELSEIF cMot == "MSSQL"
        cQuery += "Id INT IDENTITY PRIMARY KEY, "
    ELSEIF cMot == "MYSQL"
        cQuery += "Id SERIAL, "
    ENDIF

    FOR i = 1 TO LEN( aFld )
        cType = aFld[ i, DBS_TYPE ]

        DO CASE
            CASE cType = "C"
                cQuery += aFld[ i, DBS_NAME ] + " VARCHAR ( " + NTRIM( aFld[ i, DBS_LEN ] ) + " ), "
            CASE cType = "N"
                cQuery += aFld[ i, DBS_NAME ] + " NUMERIC ( " + NTRIM( aFld[ i, DBS_LEN ] ) + ", " + NTRIM( aFld[ i, DBS_DEC ] ) + " ), "
            CASE cType = "D"
                cQuery += aFld[ i, DBS_NAME ] + " DATETIME, "
            CASE cType = "L"
                cQuery += aFld[ i, DBS_NAME ] + " INT, "
            CASE cType = "M"
                IF cMot == "JET"
                    cQuery += "[" + aFld[ i, DBS_NAME ] + "]" + " MEMO, "
                ELSEIF cMot == "MSSQL"
                    cQuery += "[" + aFld[ i, DBS_NAME ] + "]" + " TEXT, "
                ELSEIF cMot == "MYSQL"
                    cQuery += aFld[ i, DBS_NAME ] + " TEXT, "
                ENDIF
        ENDCASE
    NEXT

    cQuery = STRIM( cQuery, 2 ) + " )"

    SQLEXEC( cQuery )

    RETURN NIL


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8411
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: function to create tables for Access and MySQL

Postby Antonio Linares » Sat Jun 08, 2013 9:40 pm

Enrico,

Where are functions STrim(), NTrim() and SqlExec() ? thanks
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41462
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: function to create tables for Access and MySQL

Postby Enrico Maria Giordano » Sat Jun 08, 2013 9:45 pm

Antonio,

Antonio Linares wrote:Enrico,

Where are functions STrim(), NTrim() and SqlExec() ? thanks


Code: Select all  Expand view
#define STRIM( cStr, nChr ) Left( cStr, Len( cStr ) - nChr )
#define NTRIM( nNumber ) LTrim( Str( nNumber ) )

FUNCTION SQLEXEC( cQuery )

    LOCAL cCns := "Your connectionstring here"

    LOCAL oCn := CREATEOBJECT( "ADODB.Connection" )

    oCn:CursorLocation = adUseClient

    oCn:Open( cCns )

    oCn:Execute( cQuery )

    oCn:Close()

    RETURN NIL


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8411
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: function to create tables for Access and MySQL

Postby lucasdebeltran » Sun Jun 09, 2013 6:48 pm

Mr. Enrico,

Thank you very much, but I always get, either MYSQL or MSACCESS:

Incorrect arguments, out of range or in conflict with others. (0x800A0BB9)


What I am doing wrong?.

Thank you.
Muchas gracias. Many thanks.

Un saludo, Best regards,

Harbour 3.2.0dev, Borland C++ 5.82 y FWH 13.06 [producción]

Implementando MSVC 2010, FWH64 y ADO.

Abandonando uso xHarbour y SQLRDD.
User avatar
lucasdebeltran
 
Posts: 1303
Joined: Tue Jul 21, 2009 8:12 am

Re: function to create tables for Access and MySQL

Postby Enrico Maria Giordano » Sun Jun 09, 2013 8:12 pm

Lucas,

lucasdebeltran wrote:Mr. Enrico,

Thank you very much, but I always get, either MYSQL or MSACCESS:

Incorrect arguments, out of range or in conflict with others. (0x800A0BB9)


What I am doing wrong?.

Thank you.


Please, show me how are you calling the function. A reduced and self-contained sample would be better.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8411
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: function to create tables for Access and MySQL

Postby lucasdebeltran » Sun Jun 09, 2013 8:25 pm

Mr. Enrico,

I have to open the connection using oCn2:CursorLocation = adUseClient.

I was using my own Exec function, but in yours is that set on. Now it is working fine.


Also, in MySQL, when creating a table, this sets up auto_increment feature and it is managed by MySQL:

cQuery += "ID INTEGER NOT NULL AUTO_INCREMENT, PRIMARY KEY(ID), "



What is the equivalent please for MS Access?.

For MsAccess, I tried with NO luck:

cQuery += "Id INT IDENTITY PRIMARY KEY, "


But the auto increment feature is not managed propelly, I always get 0 at the Id field. Anything else to set up?.

Thank you.

Best regards
Muchas gracias. Many thanks.

Un saludo, Best regards,

Harbour 3.2.0dev, Borland C++ 5.82 y FWH 13.06 [producción]

Implementando MSVC 2010, FWH64 y ADO.

Abandonando uso xHarbour y SQLRDD.
User avatar
lucasdebeltran
 
Posts: 1303
Joined: Tue Jul 21, 2009 8:12 am

Re: function to create tables for Access and MySQL

Postby Enrico Maria Giordano » Sun Jun 09, 2013 9:36 pm

lucasdebeltran wrote:Mr. Enrico,

I have to open the connection using oCn2:CursorLocation = adUseClient.

I was using my own Exec function, but in yours is that set on. Now it is working fine.


I don't understand but ok, that's good. :-)

lucasdebeltran wrote:Also, in MySQL, when creating a table, this sets up auto_increment feature and it is managed by MySQL:

cQuery += "ID INTEGER NOT NULL AUTO_INCREMENT, PRIMARY KEY(ID), "



What is the equivalent please for MS Access?.

For MsAccess, I tried with NO luck:

cQuery += "Id INT IDENTITY PRIMARY KEY, "


But the auto increment feature is not managed propelly, I always get 0 at the Id field. Anything else to set up?.

Thank you.

Best regards


Please look at my function:

Code: Select all  Expand view
   IF cMot == "JET"
        cQuery += "Id COUNTER PRIMARY KEY, "


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8411
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: function to create tables for Access and MySQL

Postby lucasdebeltran » Mon Jun 10, 2013 7:38 am

Mr. Enrico,

Thank you, but with your function the result is the same, with Access and ADO autoincrement fields are not managed.

With MySQL is fine.

Anything else has to be done?.

Thanks.
Muchas gracias. Many thanks.

Un saludo, Best regards,

Harbour 3.2.0dev, Borland C++ 5.82 y FWH 13.06 [producción]

Implementando MSVC 2010, FWH64 y ADO.

Abandonando uso xHarbour y SQLRDD.
User avatar
lucasdebeltran
 
Posts: 1303
Joined: Tue Jul 21, 2009 8:12 am

Re: function to create tables for Access and MySQL

Postby Richard Chidiak » Mon Jun 10, 2013 7:42 am

Lucas

What provider do you use to connect to Mysql with ADO ?

Richard
http://www.cbati.com

Uestudio
Fwh 13.05 Harbour 3.2 MSVC 2013
User avatar
Richard Chidiak
 
Posts: 946
Joined: Thu Oct 06, 2005 7:05 pm
Location: France

Re: function to create tables for Access and MySQL

Postby Enrico Maria Giordano » Mon Jun 10, 2013 8:16 am

Lucas,

lucasdebeltran wrote:Mr. Enrico,

Thank you, but with your function the result is the same, with Access and ADO autoincrement fields are not managed.

With MySQL is fine.

Anything else has to be done?.

Thanks.


My function works fine here, even with Access. As I already said, I need a sample of how are you calling it.

EMG
User avatar
Enrico Maria Giordano
 
Posts: 8411
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: function to create tables for Access and MySQL

Postby lucasdebeltran » Mon Jun 17, 2013 7:42 am

Mr. Enrico,

Thank you very much. Your function works perfect. There is a bug at TDataRow managing Access numeric fields with decimals, not related to your function.

Best regards
Muchas gracias. Many thanks.

Un saludo, Best regards,

Harbour 3.2.0dev, Borland C++ 5.82 y FWH 13.06 [producción]

Implementando MSVC 2010, FWH64 y ADO.

Abandonando uso xHarbour y SQLRDD.
User avatar
lucasdebeltran
 
Posts: 1303
Joined: Tue Jul 21, 2009 8:12 am

Re: function to create tables for Access and MySQL

Postby nageswaragunupudi » Mon Jun 17, 2013 12:14 pm

lucasdebeltran wrote:Mr. Enrico,

Thank you very much. Your function works perfect. There is a bug at TDataRow managing Access numeric fields with decimals, not related to your function.

Best regards

Yes, in the case of blank row and it was fixed. You already have the fixed version with you
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10330
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: function to create tables for Access and MySQL

Postby lucasdebeltran » Mon Jun 17, 2013 1:36 pm

Mr. Nages,

In the datarow.prg you sent me on 15th I still get the error.

The error only happens with MSACCESS, not with MySQL or MSSQL server.

Thank you.
Muchas gracias. Many thanks.

Un saludo, Best regards,

Harbour 3.2.0dev, Borland C++ 5.82 y FWH 13.06 [producción]

Implementando MSVC 2010, FWH64 y ADO.

Abandonando uso xHarbour y SQLRDD.
User avatar
lucasdebeltran
 
Posts: 1303
Joined: Tue Jul 21, 2009 8:12 am

Re: function to create tables for Access and MySQL

Postby Antonio Linares » Mon Jun 17, 2013 2:21 pm

Enrico,

Does your function support Memo fields ? How do you specify them ? thanks

Lucas,

I am using Enrico's code here and the Access tables are properly created with the autoinc counter
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41462
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: cnavarro and 130 guests