将数据插入表中时出现“无此表”错误

时间:2017-03-31 09:03:37

标签: android

获取错误,因为没有这样的表,表没有创建,请帮我解答。我认为错误是database.java文件。所以我上传了它。在插入值时我没有将电话号码字段更改为long。 ..我把它作为数据库文件中的TEXT数据类型。            “

               public class MyDatabase extends SQLiteOpenHelper {

          Context c;

         // Database Name
           public static final String DATABASE_NAME = "BIRLA_ACCOUNTS";
        //Table Name
        public static final String TABLE_NAME = "MASTERACCOUNTS";
        //  Table Columns names
        public static final String CODE = "CODE";
        public static final String NAME = "NAME";
        public static final String TYPE = "TYPE";
        public static final String ADD1 = "ADD1";
        public static final String ADD2 = "ADD2";
        public static final String CITY = "CITY";
        public static final String COUNTRY = "COUNTRY";
        public static final String PHONENO = "PHONENO";
        public static final String CONTACTPERSON = "CONTACTPERSON";
         public static final String EMAILID = "EMAILID";
        public static final String FAX = "FAX";
         public static final String TNO = "TNO";
        public static final String CSTNO = "CSTNO";
        public static final String GSTNO = "GSTNO";
        public static final String SERVICENO = "SERVICENO";

        public MyDatabase(Context context) {
        super(context, DATABASE_NAME, null, 1);
        c = context;

       }

         @Override
          public void onCreate(SQLiteDatabase db) {
          try {

            db.execSQL("CREATE TABLE "+TABLE_NAME+"("+CODE+" INTEGER                                                                                                                                                                                                                                   
                     AUTOINCREMENT,"+NAME+" TEXT PRIMARY KEY ,"+TYPE+" TEXT,  
                       "+ADD1+" TEXT,"+ADD2+" TEXT,"+CITY+" TEXT,"+COUNTRY+                                                                                                                                                                                 
                          " TEXT,"+PHONENO+" TEXT,"
                    +CONTACTPERSON+" TEXT,"+EMAILID +" VARCHAR(50),"+FAX+" 
                        REAL,"+TNO+" REAL,"+CSTNO+" REAL,"+GSTNO+" REAL,"                                                                                                                          
            +SERVICENO +" REAL)");
              Toast.makeText(c, "Table Created", Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                  Log.e("AccountsMaster creation", "" + e);
                       }


                  }

               @Override
          public void onUpgrade(SQLiteDatabase db, int oldVersion,
                   int  newVersion) {
            // Drop older table if existed
                db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
             // Creating tables again
                 onCreate(db);
              }

`

3 个答案:

答案 0 :(得分:1)

纠正您的查询。意识到 Blank Space Single-Double Quote

 "CREATE TABLE " + TABLE_NAME + " ("
                    + CODE + " INTEGER AUTOINCREMENT ," 
                    + NAME + " TEXT PRIMARY KEY ,"
                    + TYPE + " TEXT ,"
                    + ADD1 + " TEXT ,"
                    + ADD2 + " TEXT ,"
                    + CITY + " TEXT ,"
                    + COUNTRY + " TEXT ,"
                    + PHONENO + " TEXT ,"
                    + CONTACTPERSON + " TEXT ,"
                    + EMAILID + " VARCHAR(50) ,"
                    + FAX + " REAL ,"
                    + TNO + " REAL ,"
                    + CSTNO + " REAL ,"
                    + GSTNO + " REAL ,"
                    + SERVICENO + " REAL" + ")";

<强> FYI

CODE 字段中使用 INTEGER PRIMARY KEY AUTOINCREMENT

 + CODE + " INTEGER PRIMARY KEY AUTOINCREMENT ," 
 + NAME + " TEXT ,"

答案 1 :(得分:0)

enter image description here而不是AUTOINCREMENT使用AUTO_INCREMENT

答案 2 :(得分:0)

尝试添加如下查询:

String TABLE ="CREATE TABLE "+ TABLE_NAME + "("
                + CODE + " INTEGER AUTOINCREMENT,"
                + NAME + " TEXT PRIMARY KEY ,"
                + TYPE + " TEXT,"
                + ADD1 + " TEXT,"
                + ADD2 + " TEXT,"
                + CITY + " TEXT,"
                + COUNTRY + " TEXT,"
                + PHONENO + " TEXT,"
                + CONTACTPERSON + " TEXT,"
                + EMAILID + " VARCHAR(50),"
                + FAX + " REAL,"
                + TNO + " REAL,"
                + CSTNO + " REAL,"
                + GSTNO+ " REAL,"                                                                                                                          
                + SERVICENO +" REAL" +")";

        db.execSQL(TABLE);

始终关心白色空间并结束如上所述的紧密苞片。 希望这会对你有所帮助。