Giter VIP home page Giter VIP logo

Comments (2)

richecr avatar richecr commented on June 3, 2024

I believe I have this same problem. I'm studying trying to make a sql query builder for Python using gopy, but I'm having a similar problem:

panic: interface conversion: interface {} is map[string]interface {}, not *map[string]interface {}

Function causing the error:

func (qb *QueryBuilder) Exec() ([]map[string]interface{}, error) {
	qb.Compiler.SetOptionsBuilder(qb.Statements, qb.Simple)
	result, err := qb.Compiler.Exec()
	qb.Reset()
	return result, err
}

qb.Compiler.Exec() function:

func (qc *QueryCompiler) Exec() ([]map[string]interface{}, error) {
	query := qc.Simple.Raw.Sql
	if query == "" {
		query = qc.ToSQL()
	}
	qc.Reset()

	var result []map[string]interface{}

	if qc.Simple.IsDQL {
		rows, err := qc.Client.Query(query)
		if err != nil {
			return nil, err
		}
		defer rows.Close()

		cols, err := rows.Columns()
		if err != nil {
			fmt.Println(err)
		}
		colTypes, err := rows.ColumnTypes()
		if err != nil {
			fmt.Println(err)
			return nil, err
		}

		vals := make([]interface{}, len(cols))
		for i, ct := range colTypes {
			switch ct.DatabaseTypeName() {
			case "VARCHAR", "TEXT":
				vals[i] = new(string)
			case "INT":
				vals[i] = new(int)
			default:
				vals[i] = new(interface{})
			}
		}

		for rows.Next() {
			scanArgs := make([]interface{}, len(cols))
			for i := range vals {
				scanArgs[i] = &vals[i]
			}

			err = rows.Scan(scanArgs...)
			if err != nil {
				fmt.Println(err)
				continue
			}

			rowMap := make(map[string]interface{})
			for i, colName := range cols {
				valPtr := vals[i]

				var val interface{}
				switch v := valPtr.(type) {
				case *string:
					if v != nil {
						val = *v
					}
				case *int:
					if v != nil {
						val = *v
					}
				default:
					val = v
				}

				rowMap[colName] = val
			}

			result = append(result, rowMap)
		}
	} else {
		_, err := qc.Client.Exec(query)
		if err != nil {
			return nil, err
		}
	}

	fmt.Println("result", result)
	return result, nil
}

This is the complete code: https://github.com/richecr/pythonicsqlgo

from gopy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.