2016-06-16 12 views
-1

Web APIからのデータがデータベースに格納されるアプリケーションを開発しています。 マイDBHelperクラス:テーブル作成中のAndroid SQLiteの問題 "near:") ":構文エラー"

public class DBHelper extends SQLiteOpenHelper { 
    public static final String DB_NAME = "LocalVenues.db"; 
    public static final int DB_VERSION = 1; 

    public static final String COLUMN_ID = "_id"; 

    public static final String TABLE_VENUES = "venues"; 
    public static final String COLUMN_VENUE_NAME = "name"; 
    public static final String COLUMN_RATING = "rating"; 
    public static final String COLUMN_RATING_COLOR = "rating_color"; 
    public static final String COLUMN_LOCATION_ID = "location_id"; 
    public static final String COLUMN_PHOTO_ID = "photo_id"; 
    public static final String COLUMN_PHONE = "phone"; 


    //tips 
    public static final String TABLE_TIPS = "tips"; 
    public static final String COLUMN_VENUE_ID = "venue_id"; 
    public static final String COLUMN_TIP_TEXT = "text"; 
    public static final String COLUMN_AUTHOR_ID = "user_id"; 

    //locations 
    public static final String TABLE_LOCATIONS = "locations"; 
    public static final String COLUMN_ADDRESS = "address"; 
    public static final String COLUMN_LAT = "lat"; 
    public static final String COLUMN_LNG = "lng"; 


    //authors 
    public static final String TABLE_USERS = "users"; 
    public static final String COLUMN_FIRST_NAME = "first_name"; 
    public static final String COLUMN_LAST_NAME = "last_name"; 

    //photos 
    public static final String TABLE_PHOTOS = "photos"; 
    public static final String COLUMN_PREFIX = "prefix"; 
    public static final String COLUMN_SUFFIX = "suffix"; 



    private static DBHelper dbHelperInstance; 

    private DBHelper(Context context) { 
     super(context, DB_NAME, null, DB_VERSION); 
    } 

    public static synchronized DBHelper getInstance(Context context) { 
     if (dbHelperInstance == null) { 
      return dbHelperInstance = new DBHelper(context.getApplicationContext()); 
     } else { 
      return dbHelperInstance; 
     } 

    } 

    @Override 
    public void onConfigure(SQLiteDatabase db) { 
     super.onConfigure(db); 
     db.setForeignKeyConstraintsEnabled(true); 
    } 


    @Override 
    public void onCreate(SQLiteDatabase db) { 

     String STM_CREATE_TABLE = "CREATE TABLE "; 

     String STM_PRIMARY_KEY = "PRIMARY KEY "; 
     String STM_FOREIGN_KEY = " FOREIGN KEY "; 
     String STM_REFERENCES = " REFERENCES "; 
     String STM_TEXT = " TEXT "; 
     String STM_AUTOINCREMENT = " AUTOINCREMENT, "; 

     String STM_TEXT_PRIMARY_KEY = STM_TEXT + STM_PRIMARY_KEY; 
     String STM_NOT_NULL = " NOT NULL, "; 
     String STM_REAL = " REAL "; 
     String STM_INTEGER = " INTEGER "; 


     String CREATE_TABLE_VENUES = STM_CREATE_TABLE + TABLE_VENUES + " (" + 
       COLUMN_ID + STM_TEXT_PRIMARY_KEY + ", " + 
       COLUMN_VENUE_NAME + STM_TEXT + STM_NOT_NULL + 
       COLUMN_RATING + STM_REAL + STM_NOT_NULL + 
       COLUMN_RATING_COLOR + STM_TEXT + STM_NOT_NULL + 
       COLUMN_LOCATION_ID + STM_INTEGER + STM_NOT_NULL + 
       COLUMN_PHOTO_ID + STM_TEXT + STM_NOT_NULL + 
       COLUMN_PHONE + STM_TEXT + ", " + 
       STM_FOREIGN_KEY + "(" + COLUMN_LOCATION_ID + ")" + STM_REFERENCES + TABLE_LOCATIONS + "(" + COLUMN_ID + "), " + 
       STM_FOREIGN_KEY + "(" + COLUMN_PHOTO_ID + ")" + STM_REFERENCES + TABLE_PHOTOS + "(" + COLUMN_ID + ") " 
       + ");"; 

     String CREATE_TABLE_LOCATIONS = STM_CREATE_TABLE + TABLE_LOCATIONS + " (" + 
       COLUMN_ID + STM_INTEGER + STM_PRIMARY_KEY + STM_AUTOINCREMENT + 
       COLUMN_ADDRESS + STM_TEXT + STM_NOT_NULL + 
       COLUMN_LAT + STM_REAL + STM_NOT_NULL + 
       COLUMN_LNG + STM_REAL + STM_NOT_NULL 
       + ");"; 

     String CREATE_TABLE_TIPS = STM_CREATE_TABLE + TABLE_TIPS + " (" + 
       COLUMN_ID + STM_TEXT_PRIMARY_KEY + ", " + 
       COLUMN_VENUE_ID + STM_TEXT + STM_NOT_NULL + 
       COLUMN_AUTHOR_ID + STM_TEXT + STM_NOT_NULL + 
       COLUMN_TIP_TEXT + STM_TEXT + STM_NOT_NULL + 
       STM_FOREIGN_KEY + "(" + COLUMN_VENUE_ID + ")" + STM_REFERENCES + TABLE_VENUES + "(" + COLUMN_ID + "), " + 
       STM_FOREIGN_KEY + "(" + COLUMN_AUTHOR_ID + ")" + STM_REFERENCES + TABLE_USERS + "(" + COLUMN_ID + "), " + 
       STM_FOREIGN_KEY + "(" + COLUMN_PHOTO_ID + ")" + STM_REFERENCES + TABLE_PHOTOS + "(" + COLUMN_ID + ") " 
       + ");"; 

     String CREATE_TABLE_PHOTOS = STM_CREATE_TABLE + TABLE_PHOTOS + " (" + 
       COLUMN_ID + STM_TEXT_PRIMARY_KEY + ", " + 
       COLUMN_PREFIX + STM_TEXT + STM_NOT_NULL + 
       COLUMN_SUFFIX + STM_TEXT + STM_NOT_NULL 
       + ");"; 

     String CREATE_TABLE_USERS = STM_CREATE_TABLE + TABLE_USERS + " (" + 
       COLUMN_ID + STM_TEXT_PRIMARY_KEY + ", " + 
       COLUMN_FIRST_NAME + STM_TEXT + STM_NOT_NULL + 
       COLUMN_LAST_NAME + STM_TEXT + STM_NOT_NULL + 
       STM_FOREIGN_KEY + " (" + COLUMN_PHOTO_ID + ") " + STM_REFERENCES + TABLE_PHOTOS + " (" + COLUMN_ID + ") " 
       + ");"; 

     db.execSQL(CREATE_TABLE_VENUES); 
     db.execSQL(CREATE_TABLE_LOCATIONS); 
     db.execSQL(CREATE_TABLE_TIPS); 
     db.execSQL(CREATE_TABLE_PHOTOS); 
     db.execSQL(CREATE_TABLE_USERS); 

    } 

    //The simplest implementation of onUpgrade() method 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     String STM_DROP_TABLE_IF_EXISTS = "DROP TABLE IF EXISTS "; 
     if (oldVersion != newVersion) { 
      db.execSQL(STM_DROP_TABLE_IF_EXISTS + TABLE_VENUES); 
      db.execSQL(STM_DROP_TABLE_IF_EXISTS + TABLE_LOCATIONS); 
      db.execSQL(STM_DROP_TABLE_IF_EXISTS + TABLE_PHOTOS); 
      db.execSQL(STM_DROP_TABLE_IF_EXISTS + TABLE_TIPS); 
      db.execSQL(STM_DROP_TABLE_IF_EXISTS + TABLE_USERS); 
     } 
    } 
} 

私はエラーました:E/SQLiteLog: (1) near ")": syntax error を、結果として、私は別のサブ疑問を持っています。このようなSQLiteのエラーがより有益にする方法はありますか?

+0

は完全なログですか? – Raghunandan

+0

どのラインにエラーがありますか? – Lino

+0

@Lino、@ Raghunandanはい、これは実際に私が得たすべての情報であるという問題です。私のサービスでは、応答をチェックし、別のラッパークラスを使用してデータを格納します。そして、私のログ 'I/Service:探検応答はヌルです:偽'私はこの行だけを得ました。 –

答えて

2

あなたのコードを実行するとlogcat出力はLNGに余分なコンマは、REAL NOT NULLであり

CREATE TABLE locations (_id INTEGER PRIMARY KEY AUTOINCREMENT, address TEXT NOT NULL, lat REAL NOT NULL, lng REAL NOT NULL,); 

でしたが、問題はこの

String STM_NOT_NULL = " NOT NULL, "; 

です)

あなたは取り除く必要がありますその

さらにコードを実行すると

Caused by: android.database.sqlite.SQLiteException: unknown column "photo_id" in foreign key definition (code 1): , while compiling: CREATE TABLE tips (_id TEXT PRIMARY KEY , venue_id TEXT NOT NULL, user_id TEXT NOT NULL, text TEXT NOT NULL, FOREIGN KEY (venue_id) REFERENCES venues(_id), FOREIGN KEY (user_id) REFERENCES users(_id), FOREIGN KEY (photo_id) REFERENCES photos(_id)); 

テーブルの作成中にクエリに列がありません。

どのようにテーブル構造が必要かわかりません。

だから私はあなたがlogcatを見てあなたのクエリを詳しく見ることをお勧めします。あなたは、アプリケーションがクラッシュしたときにlogcatに表示されるはずの十分な詳細を投稿していません。

以下はクラッシュしません。フルクラッシュログなしで回答を投稿するのは非常に難しいです。

public class DbHelper extends SQLiteOpenHelper { 
    public static final String DB_NAME = "LocalVenues.db"; 
    public static final int DB_VERSION = 1; 

    public static final String COLUMN_ID = "_id"; 

    public static final String TABLE_VENUES = "venues"; 
    public static final String COLUMN_VENUE_NAME = "name"; 
    public static final String COLUMN_RATING = "rating"; 
    public static final String COLUMN_RATING_COLOR = "rating_color"; 
    public static final String COLUMN_LOCATION_ID = "location_id"; 
    public static final String COLUMN_PHOTO_ID = "photo_id"; 
    public static final String COLUMN_PHONE = "phone"; 


    //tips 
    public static final String TABLE_TIPS = "tips"; 
    public static final String COLUMN_VENUE_ID = "venue_id"; 
    public static final String COLUMN_TIP_TEXT = "text"; 
    public static final String COLUMN_AUTHOR_ID = "user_id"; 

    //locations 
    public static final String TABLE_LOCATIONS = "locations"; 
    public static final String COLUMN_ADDRESS = "address"; 
    public static final String COLUMN_LAT = "lat"; 
    public static final String COLUMN_LNG = "lng"; 


    //authors 
    public static final String TABLE_USERS = "users"; 
    public static final String COLUMN_FIRST_NAME = "first_name"; 
    public static final String COLUMN_LAST_NAME = "last_name"; 

    //photos 
    public static final String TABLE_PHOTOS = "photos"; 
    public static final String COLUMN_PREFIX = "prefix"; 
    public static final String COLUMN_SUFFIX = "suffix"; 



    private static DbHelper dbHelperInstance; 

    public DbHelper(Context context) { 
     super(context, DB_NAME, null, DB_VERSION); 
    } 

    public static synchronized DbHelper getInstance(Context context) { 
     if (dbHelperInstance == null) { 
      return dbHelperInstance = new DbHelper(context.getApplicationContext()); 
     } else { 
      return dbHelperInstance; 
     } 

    } 

    @Override 
    public void onConfigure(SQLiteDatabase db) { 
     super.onConfigure(db); 
     db.setForeignKeyConstraintsEnabled(true); 
    } 


    @Override 
    public void onCreate(SQLiteDatabase db) { 

     String STM_CREATE_TABLE = "CREATE TABLE "; 

     String STM_PRIMARY_KEY = "PRIMARY KEY "; 
     String STM_FOREIGN_KEY = " FOREIGN KEY "; 
     String STM_REFERENCES = " REFERENCES "; 
     String STM_TEXT = " TEXT "; 
     String STM_AUTOINCREMENT = " AUTOINCREMENT, "; 

     String STM_TEXT_PRIMARY_KEY = STM_TEXT + STM_PRIMARY_KEY; 
     String STM_NOT_NULL = " NOT NULL, "; 
     String STM_REAL = " REAL "; 
     String STM_INTEGER = " INTEGER "; 


     String CREATE_TABLE_VENUES = STM_CREATE_TABLE + TABLE_VENUES + " (" + 
       COLUMN_ID + STM_TEXT_PRIMARY_KEY + ", " + 
       COLUMN_VENUE_NAME + STM_TEXT + STM_NOT_NULL + 
       COLUMN_RATING + STM_REAL + STM_NOT_NULL + 
       COLUMN_RATING_COLOR + STM_TEXT + STM_NOT_NULL + 
       COLUMN_LOCATION_ID + STM_INTEGER + STM_NOT_NULL + 
       COLUMN_PHOTO_ID + STM_TEXT + STM_NOT_NULL + 
       COLUMN_PHONE + STM_TEXT + ", " + 
       STM_FOREIGN_KEY + "(" + COLUMN_LOCATION_ID + ")" + STM_REFERENCES + TABLE_LOCATIONS + "(" + COLUMN_ID + "), " + 
       STM_FOREIGN_KEY + "(" + COLUMN_PHOTO_ID + ")" + STM_REFERENCES + TABLE_PHOTOS + "(" + COLUMN_ID + ") " 
       + ");"; 

     String CREATE_TABLE_LOCATIONS = STM_CREATE_TABLE + TABLE_LOCATIONS + " (" + 
       COLUMN_ID + STM_INTEGER + STM_PRIMARY_KEY + STM_AUTOINCREMENT + 
       COLUMN_ADDRESS + STM_TEXT + STM_NOT_NULL + 
       COLUMN_LAT + STM_REAL + STM_NOT_NULL + 
       COLUMN_LNG + STM_REAL + "NOT NULL" 
       + ");"; 

     String CREATE_TABLE_PHOTOS = STM_CREATE_TABLE + TABLE_PHOTOS + " (" + 
       COLUMN_ID + STM_TEXT_PRIMARY_KEY + ", " + 
       COLUMN_PREFIX + STM_TEXT + STM_NOT_NULL + 
       COLUMN_SUFFIX + STM_TEXT + "NOT NUll" 
       + ");"; 

     String CREATE_TABLE_USERS = STM_CREATE_TABLE + TABLE_USERS + " (" + 
       COLUMN_ID + STM_TEXT_PRIMARY_KEY + ", " + 
       COLUMN_PHOTO_ID + STM_TEXT + STM_NOT_NULL + 
       COLUMN_FIRST_NAME + STM_TEXT + STM_NOT_NULL + 
       COLUMN_LAST_NAME + STM_TEXT + STM_NOT_NULL + 
       STM_FOREIGN_KEY + " (" + COLUMN_PHOTO_ID + ") " + STM_REFERENCES + TABLE_PHOTOS + " (" + COLUMN_ID + ") " 
       + ");"; 

     String CREATE_TABLE_TIPS = STM_CREATE_TABLE + TABLE_TIPS + " (" + 
       COLUMN_ID + STM_TEXT_PRIMARY_KEY + ", " + 
       COLUMN_PHOTO_ID + STM_TEXT + STM_NOT_NULL + 
       COLUMN_VENUE_ID + STM_TEXT + STM_NOT_NULL + 
       COLUMN_AUTHOR_ID + STM_TEXT + STM_NOT_NULL + 
       COLUMN_TIP_TEXT + STM_TEXT + STM_NOT_NULL + 
       STM_FOREIGN_KEY + "(" + COLUMN_VENUE_ID + ")" + STM_REFERENCES + TABLE_VENUES + "(" + COLUMN_ID + "), " + 
       STM_FOREIGN_KEY + "(" + COLUMN_AUTHOR_ID + ")" + STM_REFERENCES + TABLE_USERS + "(" + COLUMN_ID + "), " + 
       STM_FOREIGN_KEY + "(" + COLUMN_PHOTO_ID + ")" + STM_REFERENCES + TABLE_PHOTOS + "(" + COLUMN_ID + ") " 
       + ");"; 



     db.execSQL(CREATE_TABLE_VENUES); 
     db.execSQL(CREATE_TABLE_LOCATIONS); 
     db.execSQL(CREATE_TABLE_PHOTOS); 
     db.execSQL(CREATE_TABLE_USERS); 
     db.execSQL(CREATE_TABLE_TIPS); 


    } 

    //The simplest implementation of onUpgrade() method 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     String STM_DROP_TABLE_IF_EXISTS = "DROP TABLE IF EXISTS "; 
     if (oldVersion != newVersion) { 
      db.execSQL(STM_DROP_TABLE_IF_EXISTS + TABLE_VENUES); 
      db.execSQL(STM_DROP_TABLE_IF_EXISTS + TABLE_LOCATIONS); 
      db.execSQL(STM_DROP_TABLE_IF_EXISTS + TABLE_PHOTOS); 
      db.execSQL(STM_DROP_TABLE_IF_EXISTS + TABLE_TIPS); 
      db.execSQL(STM_DROP_TABLE_IF_EXISTS + TABLE_USERS); 
     } 
    } 
} 
+0

拡大していただきありがとうございます。私はあなたの苛立ちを理解していますが、実際に私の場合は、そのような貧弱なログを得たという主な問題があります。その理由は何でしょうか? –

1

あなたのCREATE_TABLE_LOCATIONS文字列には、ブラケットを閉じる前にカンマを持つSTM_NOT_NULLを使用しています。これにより、, lng REAL NOT NULL,);のSQL構文が生成され、無効になります。それはあなたが抱えている問題の一つかもしれません。最終的な文字列をすべてLogCatに出力するか、またはデバッグ中にそれらを見て、書式設定が正しいことを確認してください。

+0

ステートメントを記録するための素晴らしいアイデアです! –

+0

あなたも大歓迎です - 私たちの答えのタイムスタンプを見てください。私はあなたに最初に答えてくれました。それは正しいものでした。そして、後で詳細を提供するために別の回答が編集されましたが、私の努力が完全に無視されています。 – C0D3LIC1OU5

+0

何か問題があった場合は申し訳ありません。はい、あなたの答えが早かったことが分かりました。私はSOのエチケットにはかなり新しいです。そのような場合、私はあなたの答えを正しいものとして再考し、マークするべきですか?そしてもう一つの質問 - そのようなログの悪い理由は何でしょうか? –

関連する問題