How to Use ZAS: Zhou’s Adaptor of Sqlis to Access MySQL Databases

Sep 29, 2024 | Programming

If you’re looking to access MySQL databases seamlessly across various programming languages, look no further. ZAS – Zhou’s Adaptor of Sqlis simplifies how applications connect to MySQL by translating SQL syntax with ease. This blog will guide you through the process of utilizing ZAS effectively, as well as troubleshooting tips to make your experience smooth and efficient.

Understanding ZAS

ZAS is a powerful C++ library offering optimal connectivity to MySQL databases. It supports cross-platform usage and multiple programming languages, including C++, Java, and Python. Think of ZAS as a skilled translator at an international conference—while everyone speaks different languages, ZAS ensures everyone understands each other when it comes to SQL syntax.

Features of ZAS

  • Cross-platform compatibility (32/64 bit) for Linux and Windows.
  • Supports various programming languages like C++, Java, and Python.
  • Provides OTLV4-compatible interfaces.
  • Automatically translates SQL syntax, simplifying tasks for upper-level applications.
  • Excellent performance in SQL syntax analysis and optimization of the MySQL Client-Server protocol.
  • Conceals the complexities of the MySQL Client-Server protocol.
  • Good extensibility based on customer needs.
  • Stable operation after extensive testing over a year.

How ZAS Functions

The overall architecture of ZAS consists of several components, focusing on connection management and SQL syntax processing. Here’s how it works:

Process Flow

To utilize ZAS, follow these steps in sequence:

  1. Call rlogon() to parse connection strings and perform database login.
  2. Invoke open() to initialize and translate your SQL queries.
  3. Input the required placeholder values and execute your statement.
  4. Finally, fetch the results of your executed query.

Connecting to the Database

The connection string is central to establishing a link between your application and the MySQL database. The connection processor handles various formats such as TNS and DSN:

  • If the string is TNS format, it parses accordingly.
  • If it’s DSN format, it processes it directly.
  • If neither, a C++ exception is thrown.

SQL Syntax Engine

The SQL syntax engine analyzes the input SQL statement by parsing it into a syntax tree. This entails:

  • Scanning for errors in the syntax tree.
  • Translating valid statements to MySQL-style syntax.
  • Processing all placeholders before serializing the final query string for output.

Compiling and Installing ZAS

To set up ZAS, navigate to your source root directory and execute:

make clean install

This will generate the necessary libraries, which you can then link with your C++ applications or load into your Java/Python projects.

Code Examples in Different Languages

Here’s how you can interact with MySQL databases using ZAS in C++, Java, and Python.

Using ZAS with C++


// Initialize connection object and log in
zas_connect cnn(tnsFilePath,dal_mysql);

// Prepare SQL stream
zas_stream stream(cnn,true);
stream.open(0,"SELECT id, NVL(name), price FROM test_db.test_tbl WHERE id=:f1");

// Insert placeholders and execute the SQL
streams1; streams5;

// Fetch results
while (!streams[strs].eof()) {
    int id = 0;
    char name[256] = "";
    float price = 0.0;
    long size = 0L;

    streamsid; streamname; streamprice; streamsize;
    stream.flush();
    printf("%d: name: %s, price: %f, size: %ld\n", id, name, price, size);
}

Using ZAS with Java


public class TestCases {
    public static void main(String[] args) throws Exception {
        zas mz = (zas)Class.forName("zas").newInstance();
        mz.login("localhost", 3306, "root", "123");

        mz.prepare("SELECT id, NVL(name), price, size FROM test_db.test_tbl WHERE id = :f1");
        mz.insertInt(10);

        while (!mz.isEof()) {
            Integer id = 0;
            String name = "";
            Double price = 0.0;
            Long size = 0L;

            id = mz.fetchInt();
            name = mz.fetchStr();
            price = mz.fetchDouble();
            size = mz.fetchLong();

            System.out.println(id + ": name: " + name + ", price: " + price + ", size: " + size);
        }
    }
}

Using ZAS with Python


from python.zas import *

def main():
    mz = zas()
    if mz.login("localhost", 3306, "root", "123") != 0:
        print("Login failed")
        exit(-1)

    if mz.prepare("SELECT NVL(id), name, price, size FROM test_db.test_tbl WHERE id = :f1int") != 0:
        print("Prepare failed")
        exit(-1)

    mz.insert_int(10)

    while mz.is_eof() == 0:
        id = mz.fetch_int()
        name = mz.fetch_str()
        price = mz.fetch_double()
        size = mz.fetch_long()
        print(f"{id}: name: {name}, price: {price}, size: {size}")

Troubleshooting Common Issues

While working with ZAS, you may encounter some common issues. Here are potential fixes:

  • Ensure that your connection string is correct—verify whether you are using TNS or DSN formatting appropriately.
  • Check your SQL syntax for errors—this could cause unexpected behavior or exceptions.
  • Make sure you have linked the library correctly in your project settings.
  • If you face connectivity issues, ensure that the MySQL server is running and accessible at the specified host and port.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox