我有3节课。
一个设置数据库和表,工作正常。 DBHelper.java
private static final String TAG = "DBHelper";
public static final String DB_NAME = "pat_test.db";
public static final String TABLE5 = "sites";
public static final String S_ID = BaseColumns._ID;
public static final String S_CLIENT = "client_of_site";
public static final String S_POSTCODE = "site_postode";
public static final String S_CON_NAME = "contact_name";
public static final String S_CON_NUM = "contact_tel_num";
public static final String S_NAME = "site_name";
public static final String S_LAST_TEST = "last_test_date";
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql5 = String.format("create table %s (%s INT PRIMARY KEY, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s INT, %s TEXT)",TABLE5, S_ID, S_CLIENT, S_NAME, S_POSTCODE, S_CON_NAME,S_CON_NUM,S_LAST_TEST);
Log.d(TAG, "onCreate: " + sql5);
db.execSQL(sql5);
}
下一个功能DBControl.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.provider.BaseColumns;
public class DBControl {
public static final String TABLE5 = "sites";
public static final String S_ID = BaseColumns._ID;
public static final String S_CLIENT = "client_of_site";
public static final String S_NAME = "site_name";
public static final String S_POSTCODE = "site_postode";
public static final String S_CON_NAME = "contact_name";
public static final String S_CON_NUM = "contact_tel_num";
public static final String S_LAST_TEST = "last_test_date";
private Context context;
private SQLiteDatabase database;
private DBHelper dbhelper;
public DBControl(Context context) {
this.context = context;
}
public DBControl open() throws SQLiteException {
dbhelper = new DBHelper(context);
database = dbhelper.getWritableDatabase();
return this;
}
public void close() {
dbhelper.close();
}
public ContentValues createContentValues3(String CLIENT,
String NAME, String POSTCODE, String CON_NAME,
long CON_NUM) {
ContentValues values = new ContentValues();
values.put(S_CLIENT, CLIENT);
values.put(S_POSTCODE, POSTCODE);
values.put(S_CON_NAME, CON_NAME);
values.put(S_CON_NUM, CON_NUM);
values.put(S_NAME, NAME);
return values;
}
public long addSiteDetails( String CLIENT, String NAME,
String POSTCODE, String CON_NAME, int CON_NUM ) {
ContentValues siteValues = createContentValues3( CLIENT, NAME,
POSTCODE, CON_NAME, CON_NUM );
return database.insert(TABLE5, null, siteValues);
}
public boolean updateSiteDetails(long id, String CLIENT, String NAME,
String POSTCODE, String CON_NAME , int CON_NUM
) {
ContentValues siteUpdateValues = createContentValues3( CLIENT, NAME,
POSTCODE, CON_NAME, CON_NUM);
return database.update(TABLE5, siteUpdateValues, S_ID + "=" + id, null) > 0;
}
}
public long fetchSiteIdByName(String NAME){
Cursor dbCursor;
long id = 0;
try {
dbCursor = database.query(true, TABLE5, new String []{S_ID}, S_NAME + "= " + NAME , null, null, null, null, null);
dbCursor.moveToFirst();
id = dbCursor.getLong(dbCursor.getColumnIndex(S_ID)); }
catch (SQLiteException e) {
id = -1;
}
return id;
}
我的第三堂课用于打电话和检查。但这是我的问题出现的地方或上一课中的fetchSiteIdByName。
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class activityStartNewTest extends Activity implements OnClickListener {
Button buttonBeginTesting;
private AutoCompleteTextView clientInput; //Refers to input box for client
private AutoCompleteTextView siteInput; //Refers to input box for site
private AutoCompleteTextView postcodeInput; //Refers to input box for postcode
private EditText nameInput; //Refers to input box for contact name
private EditText telInput; //Refers to input box for contact number
// private EditText jobrefInput; //Refers to input box for Job reference number
private DBControl dbControl;
@ Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_new_test);
dbControl = new DBControl(this);
clientInput = (AutoCompleteTextView) findViewById(R.id.autoCompleteClientNameEdit);
siteInput = (AutoCompleteTextView) findViewById(R.id.autoCompleteSiteNameEdit);
postcodeInput = (AutoCompleteTextView) findViewById(R.id.autoCompletePostcodeEdit);
nameInput = (EditText) findViewById(R.id.editTextContactName);
telInput = (EditText) findViewById(R.id.editTextContactNum);
// jobrefInput = (EditText)findViewById(R.id.editTextJobRef);
buttonBeginTesting = (Button) findViewById(R.id.buttonBeginTesting);
buttonBeginTesting.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void onClick(View arg) {
String clientData = clientInput.getText().toString();
String siteData = siteInput.getText().toString();
String postcodeData = postcodeInput.getText().toString();
String nameData = nameInput.getText().toString();
String telData = telInput.getText().toString();
// String jobrefData = jobrefInput.getText().toString();
Toast toastNotice = new Toast(this);
Dialog notice = new Dialog(this); // this needs the textview below for displaying
TextView msgBody = new TextView(this);
msgBody.setTextSize(20);
long tempValue = 0;
switch(arg.getId()){
case R.id.buttonBeginTesting:
try{
int telDataAsNum = Integer.parseInt(telData);
dbControl.open();
if((tempValue = dbControl.fetchSiteIdByName(siteData)) != -1) {
if(dbControl.updateSiteDetails(tempValue, clientData, siteData, postcodeData, nameData, telDataAsNum)){
notice.setTitle("Site Updated!");
msgBody.setText("Site detail have been updated instead");
}
else{
notice.setTitle("Update failed!");
msgBody.setText("Site already Exists, but failed!");
}
}
else{
long siteID = 0;
siteID = dbControl.addSiteDetails( clientData, siteData, postcodeData, nameData, telDataAsNum );
notice.setTitle("Site Added");
msgBody.setText("Site added at row "+ siteID);
}
dbControl.close();
}
catch (SQLiteException e){ // Notifies user if SQL error occurred
e.printStackTrace();
notice.setTitle("Insert failed");
msgBody.setText("SQL Error!");
}
catch (NumberFormatException e){ // Notifies user if NUM format error occurred
e.printStackTrace();
notice.setTitle("Insert failed");
msgBody.setText("Contact number must be a number!");
}
notice.setContentView(msgBody);
notice.show();
}
}
}
现在当我运行应用程序时,我插入数据库中,新的详细信息absolutley很好。 使用adb我拉数据库并在sqlite3中打开。它显示所有条目在第一列中为NULL,在最后一列中为NULL(最后一个尚未发布)。 我在editbox siteData的字段中使用相同的文本,它只是添加了一个新条目,并且不会更新之前的条目。
log cat显示SQLiteLog(1)没有这样的列:xxx,其中xxx是autocompleteTextView的入口。
为什么要查找以输入的文本命名的列?
答案 0 :(得分:2)
使用单引号在fetchSiteIdByName()中包围NAME可能会有所帮助。
dbCursor = database.query(true, TABLE5, new String []{S_ID}, S_NAME + "= '" + NAME + "'" , null, null, null, null, null);
我不太确定它是否适用于SQLite,但我在MySQL中遇到过同样的问题而且它有所帮助。此外,当您在数据库中获取非数字值时,用单引号将它们包围起来。
希望它有所帮助。 :)
答案 1 :(得分:1)
看来我有3个不同的问题,但是使用Lokesh的链接和帖子可以帮助我解决问题。
我将DBelper.java中的SQL字符串修改为此
String sql5 = String.format("create table %s (%s INTEGER PRIMARY KEY **AUTOINCREMENT**, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s INT, %s TEXT)",
TABLE5, S_ID, S_CLIENT, S_NAME, S_POSTCODE, S_CON_NAME,
S_CON_NUM, S_LAST_TEST);
并添加到DBControl.java类
**if**(dbCursor.moveToFirst()) id = dbCursor.getLong(dbCursor.getColumnIndex(S_ID));
在moveToFirst()之后删除分号。就像我说我是一个真正的菜鸟,但相信这是正确的。
当我卸载并重新安装应用程序时,添加AUTOINCREMENT有一个显着的效果(我本来可以更改数据库版本,如果我也想,我知道)。
当重建表时,他们没有我之前见过的列_id。所以我认为SQL字符串是我失败的原因。我确定我听说PRIMARY KEY basecolumns._ID不需要它,但现在我知道它是。
感谢Lokesh的指示。没有一些指导,我无法找到问题,我真的很感激。
我尝试添加到向上箭头但尚未获得足够的评分。卫生署。
希望这有助于他人。