's插入联系人'android

时间:2015-08-09 18:20:50

标签: android sqlite

在我的SQLite适配器类

public class SqLiteAdapter
{

MySqliteHelper helper;
Context context;

SqLiteAdapter(Context context)
{
this.context = context;
helper = new MySqliteHelper(context);
}


public long insert(String first, String last, int num)
{
SQLiteDatabase database = helper.getWritableDatabase();
Toast.makeText(context, "database found", Toast.LENGTH_SHORT).show();
ContentValues values = new ContentValues();

values.put(MySqliteHelper.COLUMN_FIRST, first);
values.put(MySqliteHelper.COLUMN_LAST, last);
values.put(MySqliteHelper.COLUMN_PHO_NUM, num);

long id = database.insert(MySqliteHelper.TABLE_NAME, null, values);
return id;

}





 static class MySqliteHelper extends SQLiteOpenHelper
{
Context context;
private static final String DATABASSE_NAME = "contact";
private static final int VERSION = 1;
private static final String TABLE_NAME = "contacts";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_FIRST = "firstName";
private static final String COLUMN_LAST = "lastName";
private static final String COLUMN_PHO_NUM = "phoneNumber";
private static final String CREATE_TABLE = "create table" +TABLE_NAME +
"(" + COLUMN_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," +
COLUMN_FIRST + "TEXT, " + COLUMN_LAST + "TEXT," + COLUMN_PHO_NUM +       "INTEGER"     + ");";




MySqliteHelper(Context context)
{
super(context, DATABASSE_NAME, null, 1);
Toast.makeText(context, "helper class created", Toast.LENGTH_SHORT).show();
this.context = context;
}


@Override
public void onCreate(SQLiteDatabase database)
{
Toast.makeText(context, "on create called", Toast.LENGTH_SHORT).show();
try
 {
database.execSQL(CREATE_TABLE);
} catch (SQLException e){
Toast.makeText(context, "Failed Creating Table", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}


}

@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int  newVersion)
{
database.execSQL("DROP TABLE IF EXIST" + TABLE_NAME);
onCreate(database);
}

   }
}

在我的MainAcivity中

public class MainActivity extends AppCompatActivity
{
EditText enterFirstName;
EditText enterLastName;
EditText enterNumber;



@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enterFirstName = (EditText) findViewById(R.id.enterFirstName);
enterLastName = (EditText) findViewById(R.id.enterLastName);
enterNumber = (EditText) findViewById(R.id.enterNumber);


}


public void addContact(View view)
{
String first = enterFirstName.getText().toString();
  String last = enterLastName.getText().toString();
 int num = Integer.parseInt(enterNumber.getText().toString());
SqLiteAdapter adapter = new SqLiteAdapter(this);
 long id = adapter.insert(first, last, num);
}

和错误  “编译时没有这样的表联系人(code1):插入联系人等等。”

我只是潜入我的应用程序的数据库存储(哦,幸运的是)我已经得到了我所遵循的所有教程的错误,现在甚至在我自己的代码中。研究这个主题似乎我试图在数据库创建之前写入数据库,所以我设置了Toasts来跟踪流程,它的进展似乎并没有像我在它创建之前写的那样,但是idk我我是个初学者。所以你们为什么认为我收到这个错误呢?我提前为草率编辑道歉,并提前感谢您的任何帮助,以便我可以开始编写更好的代码= D!

1 个答案:

答案 0 :(得分:2)

试试这个

change your db string to this

"create table " +TABLE_NAME +"(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
            COLUMN_FIRST + " TEXT, " + COLUMN_LAST + " TEXT," + COLUMN_PHO_NUM +       " INTEGER"     + ");";