Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

database schema has changed

Tags:

c#

sqlite

I keep getting this error database schema has changed near "1": syntax error.

I am not changing my schema in the code. This is the code where I make the schema. wish to try each answer in order that you posted it. This code is used to transfer a database to another database program. So it has to be compatible with more then one database program.

public DataTable GetSchemaTable(string schema, string nameTable)
    {
        switch (m_dbType)
        {
            case dbTypeEnum.Sqlite:
                //Make the schema of the source table
                MakeConnectionString();
                string fullTableName = schema.Length > 0
                    ? string.Format("[{0}].[{1}]", schema, nameTable)
                    : string.Format("[{0}]", nameTable);
                if (!string.IsNullOrEmpty(fullTableName))
                {
                    string sql = string.Format("SELECT * FROM {0} LIMIT 1", fullTableName);
                    DataTable dtSchemaSource;
                    try
                    {
                        using (IDataReader rdr = ReadOnlyForwardOnly(sql))
                        {
                            dtSchemaSource = rdr.GetSchemaTable();
                        }
                    }
                    finally
                    {
                        CloseConnection();
                    }
                    if (dtSchemaSource == null)
                    {
                        throw new RadGeneralException("rdr.GetSchemaTable() returns null with sql = " + sql);
                    }

                    return dtSchemaSource;
                }
                break;

            default:
                //Make the schema of the source table
                MakeConnectionString();
                string fullTableName = schema.Length > 0
                    ? string.Format("[{0}].[{1}]", schema, nameTable)
                    : string.Format("[{0}]", nameTable);
                string sql = string.Format("SELECT TOP 1 * FROM {0}", fullTableName);
                DataTable dtSchemaSource;
                try
                {
                    using (IDataReader rdr = ReadOnlyForwardOnly(sql))
                    {
                        dtSchemaSource = rdr.GetSchemaTable();
                    }
                }
                finally
                {
                    CloseConnection();
                }
                if (dtSchemaSource == null)
                {
                    throw new RadGeneralException("rdr.GetSchemaTable() returns null with sql = " + sql);
                }
                return dtSchemaSource;

        }
    }

This is the part where the sqlite schema has changed arror occurs.

StringBuilder sbColList = new StringBuilder();
        nCol = 0;
        identityColInBothTables = false;
        //Make the schema of the source table
        DataTable dtSchemaSource = objDbSource.GetSchemaTable(SchemaSource, Name);
        //Make the schema of the target table
        DataTable dtSchemaTarget = objDbTarget.GetSchemaTable(SchemaTarget, Name);
like image 780
Petah Avatar asked Sep 02 '15 06:09

Petah


2 Answers

I believe that instead of SELECT TOP 1 * you need to be using SELECT * FROM .... LIMIT 1

like image 97
b0redom Avatar answered Oct 23 '22 18:10

b0redom


You need to check nameTable is null or empty

if(!string.IsNullOrEmpty(fullTableName))
 string sql = string.Format("SELECT * FROM {0} LIMIT 1", fullTableName);

for SQLLITE

SELECT * FROM Table_Name LIMIT 1;

for SQL

SELECT TOP 1 * FROM Table_Name;
like image 42
Nalaka Avatar answered Oct 23 '22 19:10

Nalaka