我正在尝试在我的 sqlite 数据库中添加新记录。当我使用 sqlite manager 看到数据库时,我没有收到任何错误,但也没有在数据库中看到任何类型的记录,下面是我编写的代码。请帮助我摆脱这个问题。
以下是我的数据库处理程序代码..
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------");
}
}
);