Petah
Petah

Reputation: 127

database schema has changed

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);

Upvotes: 5

Views: 5555

Answers (3)

Nikita Shrivastava
Nikita Shrivastava

Reputation: 3018

Can you try this:

SELECT * FROM SAMPLE_TABLE ORDER BY ROWID ASC LIMIT 1

Upvotes: 1

Nalaka
Nalaka

Reputation: 1185

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;

Upvotes: 1

b0redom
b0redom

Reputation: 387

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

Upvotes: 5

Related Questions