2012-11-19 78 views
3

db.insert函數只在表中插入一個自動遞增的ID,沒有別的,但我從活動編輯文本中獲取值。錶行的單元格的其餘部分保持爲空db.insert只在數據庫中存儲自動遞增的ID

這裏是我的代碼:

public class SHelper extends SQLiteOpenHelper { 

    static final String dbName = "School"; 
    static final String studentTable = "Student"; 
    static final String colName = "Name"; 
    static final String colPassword = "Password"; 
    public static final int dbVersion=1; 

    public SHelper(Context context) 
    { 
     super(context, dbName, null, dbVersion); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("CREATE TABLE "+studentTable+"("+colID+" INTEGER PRIMARY KEY, "+colName+" TEXT,"+colPassword+" TEXT);"); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS " + studentTable); 
     onCreate(db); 
    } 

    public void add(StudentData studentdata) 
    { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 

     values.put(colName, StudentData.getName()); 
     values.put(colPassword, StudentData.getPassword()); 
     db.insert(studentTable, null, values); 
     db.close(); 
    } 


} 

public class StudentData { 

    private static String name; 
    private static String password; 

    public StudentData() { 
     super(); 
    } 

    public StudentData(String name,String password) { 
     super(); 

     StudentData.name = name; 
     StudentData.password = password; 
    } 

    public static String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     StudentData.name = name; 
    } 

     public static String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     StudentData.password = password; 
    } 
} 

public class stoActivity extends Activity { 

    SHelper db = new SHelper(this); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_sto); 

     final EditText fullname=(EditText)findViewById(R.id.fullName); 
     final EditText password=(EditText)findViewById(R.id.password); 

     final String fullname=fullname.getText().toString(); 
     final String password=password.getText().toString(); 

     Button registerbutton = (Button) findViewById(R.id.registerButton);  
     registerbutton.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v){ 

      db.add(new StudentData(fullname,password)); 

      Intent it=new Intent(stoActivity.this,otherActivity.class); 
      stoActivity.this.startActivity(it); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_register, menu); 
     return true; 
    } 
} 
+0

它會拋出任何異常嗎? –

回答

1

有您的問題:

public void add(*StudentData studentdata) 
    { 
     ...  
     values.put(colName, *StudentData.getName()); 
     values.put(colPassword, *StudentData.getPassword()); 
     ... 
    } 

你能看到嗎?你的陳述是指簽名的錯誤部分。您需要將方法主體中的StudentData更改爲小寫studentdata,以將傳遞給方法的內容作爲參數進行匹配。這種非法行爲經過了編譯,因爲你在我不確定的地方有靜態修飾符,例如。 getName()getPassword()方法。

0

創建表自動遞增鍵,所以改變方法的onCreate以下幾點:

@Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("CREATE TABLE "+studentTable+"("+colID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+colName+" TEXT,"+colPassword+" TEXT);"); 

    } 
0

不插入您已聲明爲主鍵的列的任何值。

0

這只是一個假設,但我有類似的問題,如果我想保存數據在數據庫。在db.execSQL的onCreate-Method中,在逗號 - >「INTEGER PRIMARY KEY」後出現空白。我知道SQLite對於這種輸入非常敏感,您必須精確地查看空白,逗號或任何其他輸入。