2
我想在我的sqlite數據庫中添加新記錄。我沒有得到任何錯誤,但也沒有看到數據庫中的任何類型的記錄,當我看到數據庫使用sqlite經理 以下是我寫的代碼。 PLZ幫助我擺脫這個問題。無法添加新的記錄到sqlite數據庫android
以下是我的數據庫處理程序的代碼..
enter code here
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
public class DatabaseHandler extends SQLiteOpenHelper
{
private static final int DATABASE_VERSON=1;
private static final String DATABASE_NAME="Info_manager";
private static final String TABLE_DETAILS = "info_detail";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_FNAME = "f_name";
private static final String KEY_LNAME = "l_name";
private static final String KEY_PH_NO = "c_num";
private static final String KEY_EMAIL = "e_mail";
public DatabaseHandler(Context context)
{
super(context,null , null, 1);
}
@Override
public void onCreate(SQLiteDatabase db)
{
//db = this.getWritableDatabase();
///System.out.println("----333------");
String CREATE_DETAILS_TABLE = "CREATE TABLE " + TABLE_DETAILS
+ "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_FNAME + " TEXT," + KEY_LNAME + " TEXT," + KEY_PH_NO + " TEXT," + KEY_EMAIL + " TEXT " + ")";
db.execSQL(CREATE_DETAILS_TABLE);
///System.out.println("----444------");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DETAILS);
// Create tables again
onCreate(db);
}
// Adding new contact
void addContact(Contact contact)
{
SQLiteDatabase db = this.getWritableDatabase();
// ContentValues values = new ContentValues();
String sql;
sql = "INSERT INTO info_detail (f_name, l_name, c_num, e_mail) " +
"VALUES ('" + contact.getfName().toString() + "', '" + contact.getlName().toString() + "', '" + contact.getContactNumber().toString()+ "', '" + contact.getEmailAddress().toString() +"')";
db.execSQL(sql);
/*values.put(KEY_FNAME, contact.getfName());
values.put(KEY_LNAME, contact.getlName());
values.put(KEY_PH_NO, contact.getContactNumber());
values.put(KEY_EMAIL, contact.getEmailAddress());*/
// Inserting Row
System.out.print("----inserting----");
//db.insert(TABLE_DETAILS, null, values);
System.out.print("----inserted----");
db.close();
}
}
和下面是我把它叫做處理器
public class SQLitelActivity extends Activity implements OnClickListener
{
EditText fname,lname,con_num,email_adr;
Button savebtn,viewbtn;
DatabaseHandler db;
//SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sqlitel);
fname=(EditText)findViewById(R.id.fname);
lname=(EditText)findViewById(R.id.lname);
con_num=(EditText)findViewById(R.id.cnumber);
email_adr=(EditText)findViewById(R.id.email);
savebtn=(Button)findViewById(R.id.save);
viewbtn=(Button)findViewById(R.id.view);
db=new DatabaseHandler(this);
savebtn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
//System.out.println("----111------");
db.addContact(new Contact(fname.getText().toString()
, lname.getText().toString(),con_num.getText().toString(),email_adr.getText().toString()));
// System.out.println("----222------");
}
}
);
它的工作原理... Thanx –