我尝试插入 db.insert(inputfname,inputlname,inputnickname); 但是它是错误的。告诉我 “方法insertData(EditText,EditText,EditText)未定义类型FriendsDB insertActivity.java”你能告诉我吗
这是我的代码(Mainactivity) 包com.example.sqlite;
import java.security.PublicKey;
import com.example.sqlite.db.FriendsDB;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class insertActivity extends Activity {
private Context context;
private FriendsDB db;
EditText inputfname;
EditText inputlname;
EditText inputnickname;
Button save;
Button cancle;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.insert_layout);
//view matching
inputfname = (EditText)findViewById(R.id.fname);
inputlname = (EditText)findViewById(R.id.lname);
inputnickname = (EditText)findViewById(R.id.nickname);
save = (Button)findViewById(R.id.save);
cancle = (Button)findViewById(R.id.cancle);
//context
context = this;
db = new FriendsDB(context);
//button event
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent insertData = new Intent(insertActivity.this,FriendsListActivity.class);
insertData.putExtra("fname", inputfname.getText().toString());
insertData.putExtra("lname", inputlname.getText().toString());
insertData.putExtra("nickname", inputnickname.getText().toString());
if(insertData!=null){
db.insert(inputfname, inputlname, inputnickname); <<<<<< ERROR
}
}
} );
这是我的FriendsDB代码
package com.example.sqlite.db;
import java.util.ArrayList;
import com.example.sqlite.entry.FriendEntry;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
//ใช้ในการ inert update delete
public class FriendsDB {
private FriendsDBHelper helper;
private SQLiteDatabase db;
public FriendsDB(Context context){
helper = new FriendsDBHelper(context);
db = helper.getWritableDatabase();
}
//insert
public long insert(String fname,String lname,String nickname){
ContentValues values = new ContentValues();
values.put(FriendsDBHelper.TABLE_KEY_FNAME, fname);
values.put(FriendsDBHelper.TABLE_KEY_LNAME, lname);
values.put(FriendsDBHelper.TABLE_KEY_NICKNAME, nickname);
return db.insert(FriendsDBHelper.TABLE_NAME, null ,values);
}
//select all
public ArrayList<FriendEntry> selectAll(){
ArrayList<FriendEntry> friends = new ArrayList<FriendEntry>();
// cursor คือ ชุดข้อมูล
Cursor cursor = db.rawQuery("SELECT*FROM"+FriendsDBHelper.TABLE_NAME+"WHERE id != ?",new String[]{Integer.toString(0)});
cursor.moveToFirst();
if(cursor.getCount()!=0){
do{
FriendEntry friend = new FriendEntry();
friend.setId(cursor.getInt(cursor.getColumnIndex(FriendsDBHelper.TABLE_KEY_ID)));
friend.setFname(cursor.getString(cursor.getColumnIndex(FriendsDBHelper.TABLE_KEY_FNAME)));
friend.setLname(cursor.getString(cursor.getColumnIndex(FriendsDBHelper.TABLE_KEY_LNAME)));
friend.setNickname(cursor.getString(cursor.getColumnIndex(FriendsDBHelper.TABLE_KEY_NICKNAME)));
friends.add(friend);
}while(cursor.moveToNext());
}
return friends;
}
答案 0 :(得分:1)
更改此
db.insert(inputfname, inputlname, inputnickname);
到
db.insert(inputfname.getText().toString(), inputlname.getText().toString(), inputnickname.getText().toString());
您的数据库插入方法将字符串作为参数。
insert(String fname,String lname,String nickname)// arguments are strings