在logCat中发现缺少创建的数据库

时间:2014-02-20 15:24:09

标签: android android-sqlite

我正在尝试创建一个简单的界面来查询用户的用户名。如果它已存在于SQLite数据库中,则会找到它。

我已经创建了数据库,但LogCat告诉我,我在查询中要求的表不存在(No such table)。

这是我创建简单数据库的类DataBaseHelper

         package com.example.cinemaodeon;

       import android.content.Context;
   import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;

      public class DatabaseHelper extends SQLiteOpenHelper {
            private static final String DB_NAME = "nome_db";
            private static final int DB_VERSION = 1;
            public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        }
    @Override
    public void onCreate(SQLiteDatabase db) {
    // Creazione delle tabelle
        String sql = "";

        sql += "DROP TABLE IF EXISTS T_UTENTI";
        db.execSQL(sql);

            sql ="";
            sql += "CREATE TABLE T_UTENTI(";
        sql += " _id_user INTEGER PRIMARY KEY,";
        sql += " username TEXT NOT NULL";
        sql += " password TEXT NOT NULL";
        sql += ")";         
        db.execSQL(sql);
        sql = "";
        sql +="COMMIT";
        db.execSQL(sql);
                }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int   newVersion) {
    // Aggiornamento delle tabelle
    }
         }

我正在尝试使用点击按钮的查询来阅读它。 这是活动的代码:

   package com.example.cinemaodeon;
      import......

   public class Main extends Activity {

private DatabaseHelper DbHelper;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    DbHelper = new DatabaseHelper(this);

    setContentView(R.layout.layout_main);

    final Toast toast = Toast.makeText(this,
            "Il nome utente non è presente!!! registra un nuovo nome utente!",
            Toast.LENGTH_LONG);
    final Toast toast2 = Toast.makeText(this,
            "Il nome utente è già presente!!! Accedi con questo nome utente!",
            Toast.LENGTH_LONG);


    Button btnGO = (Button) findViewById(R.id.main_btnentra);
     btnGO.setOnClickListener(new OnClickListener(){    
        @Override    
        public void onClick(View arg0) {    

            EditText editusername =(EditText) findViewById(R.id.main_editusername);
            final String username = editusername.getText().toString();

以下代码是返回错误No such table T_UTENTI

的查询
            SQLiteDatabase db = DbHelper.getReadableDatabase();
            String[] columns = {"username"};
            Cursor cursor = db.query("T_UTENTI", columns, null, null, null, null, null);

            boolean ctrl=false; 

            while (cursor.moveToNext()) {
                if (username==cursor.getString(0)){
                    ctrl=true;
                    Intent openProgrammazione = new Intent(Main.this,Programmazione.class);  
                    startActivity(openProgrammazione); 
                    break;
                }
            }           
            if (ctrl==false){   
            toast.show();
            }

                }
            });    


     Button btnReg = (Button) findViewById(R.id.main_btnregistra);
     btnReg.setOnClickListener(new OnClickListener(){    
        @Override    
        public void onClick(View arg0) {    

            EditText editusername =(EditText) findViewById(R.id.main_editusername_registra);
            final String username = editusername.getText().toString();

            SQLiteDatabase db = DbHelper.getReadableDatabase();
            String[] columns = { "username" };
            Cursor cursor = db.query("T_UTENTI", columns, null, null, null, null, null);

            boolean ctrl=false; 

            while (cursor.moveToNext()) {
                if (username==cursor.getString(0)){
                    toast2.show();
                    ctrl=true;
                    break;
                }

            }           
            if (ctrl==false){   
            WriteUser(username);

            Intent openProgrammazione = new Intent(Main.this,Programmazione.class);  
            startActivity(openProgrammazione);  
            }
            }

     });    


}

    protected void WriteUser(String user){
            SQLiteDatabase dbw = DbHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("username", user);
            values.put("password","0000");
            @SuppressWarnings("unused")
            long id = dbw.insert("T_UTENTI", null, values);


        }

1 个答案:

答案 0 :(得分:1)

你在这里忘了一个逗号(在onCreate中):

sql += " username TEXT NOT NULL";

应该是:

sql += " username TEXT NOT NULL, ";

由于SQL命令错误,因此不会创建表 没有表格,你会得到错误“没有这样的表T_UTENTI”