我想在我的sqlite数据库中添加新记录。当我看到数据库使用sqlite管理器时,我没有收到任何错误但也没有在数据库中看到任何类型的记录 以下是我写的代码。 PLZ帮助我摆脱这个问题。
以下是我的数据库处理程序代码..
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------");
}
}
);
答案 0 :(得分:3)
这里是修改后的代码,
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
您在超级构造函数中错过了数据库名称。
我正在使用以下方法将联系人添加到数据库,
// Adding new contact
void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_FNAME, contact.getfName().toString()); // Contact First Name
values.put(KEY_LNAME, contact.getlName().toString()); // Contact Last Name
values.put(KEY_PH_NO, contact.getContactNumber().toString()); // Contact Number
values.put(KEY_EMAIL, contact.getEmailAddress().toString() ); // Contact Email
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}
我希望这会对你有所帮助。感谢。