问题描述:

I am struggling a bit with a compiler warning. Here the code:

 const char *sql;

switch (fromVersion) {

case 0:

{

sql = "ALTER TABLE abiliator_options ADD COLUMN new_column00 TEXT NOT NULL DEFAULT 'migrated from version 0'";

}

case 1:

{

sql = "ALTER TABLE abiliator_options ADD COLUMN new_column01 TEXT NOT NULL DEFAULT 'migrated from version 1'";

}

case 2:

{

sql = "ALTER TABLE abiliator_options ADD COLUMN new_column02 TEXT NOT NULL DEFAULT 'migrated from version 2'";

}

case 3:

{

sql = "ALTER TABLE abiliator_options ADD COLUMN new_column03 TEXT NOT NULL DEFAULT 'migrated from version 3'";

}

case 4:

{

sql = "ALTER TABLE abiliator_options ADD COLUMN new_column04 TEXT NOT NULL DEFAULT 'migrated from version 4'";

}

case 5:

{

sql = "ALTER TABLE abiliator_options ADD COLUMN new_column05 TEXT NOT NULL DEFAULT 'migrated from version 5'";

}

}

sqlite3_stmt *selectstmt;

if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK)

{

if (sqlite3_step(selectstmt) == SQLITE_DONE)

{

NSLog(@"Alter statement successful");

[self setDatabaseSchemaVersion];

}

else {

NSLog(@"Failed to alter the table with message '%s'.", sqlite3_errmsg(database));

}

}

else {

NSLog(@"Failed to prepare the statement with message '%s'.", sqlite3_errmsg(database));

}

sqlite3_finalize(selectstmt);

First warning is Value stored to 'sql' is never read for all of the sql var assignments, except the last one in the switch (case 5). Case 5 does not result in a warning.

Second warning is Function call argument is an uninitialized value. This is for the

if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK)

statement.

Thanks in advance for your hints.

网友答案:
  1. This is because you are missing break; statements between your cases.
  2. This is because you are missing a default:

Compiler tells you that when your fromVersion is, for example, 4, whatever you assign to it in the case 4: will get immediately overwritten by case 5:, because there is no break. It also tells you that when fromVersion is negative or more than 5, your sql is uninitialized.

网友答案:

You're missing the breaks after each case. Only the last assignment is being used.

网友答案:

Add a break; statement after each case. Further try adding a default case.

网友答案:

Switch statement Structure:- You are missing "break" and " default:"

switch (expression)

{ case match1:

      statements

      break;

 case match2:

      statements

      break;

 default:

      statements

      break;

}

网友答案:

The warning is most probably because you're not using break; in the switch (you're setting new values over the old ones, without using them).

switch (fromVersion) {
case 0:
{
    sql = "ALTER TABLE abiliator_options ADD COLUMN new_column00 TEXT NOT NULL DEFAULT 'migrated from version 0'";
    break;
}
case 1:
{
    sql = "ALTER TABLE abiliator_options ADD COLUMN new_column01 TEXT NOT NULL DEFAULT 'migrated from version 1'";
    break;
}
...
相关阅读:
Top