DatabaseHelper.java
package com.exampl.mysql;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class DatabaseHelper extends SQLiteOpenHelper{
private Context mContext;
public static final String CREATE_TABLE_SQL="create table dict(_id integer primary key autoincrement,word text,detail text);";
public DatabaseHelper(Context context,String name,CursorFactory factory,int version)
{
super(context,name,factory,version);
mContext=context;
}
public void onCreate(SQLiteDatabase db)
{
db.execSQL(CREATE_TABLE_SQL);
Toast.makeText(mContext,"創建成功!",Toast.LENGTH_LONG).show();
}
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{
}
}
MainActivity.java
package com.exampl.mysql;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
private Button insert=null;
private Button search=null;
private EditText word=null;
private EditText detail=null;
private EditText key=null;
DatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper=new DatabaseHelper(this,"myDict.db3",null,1);
insert=(Button)findViewById(R.id.insert);
insert.setOnClickListener(this);
search=(Button)findViewById(R.id.search);
search.setOnClickListener(this);
word=(EditText)findViewById(R.id.word);
detail=(EditText)findViewById(R.id.detail);
key=(EditText)findViewById(R.id.key);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.insert:
String wor=word.getText().toString().trim();
String det=detail.getText().toString().trim();
insertData(dbHelper.getReadableDatabase(),wor,det);
Toast.makeText(this, "添加生詞成功!", Toast.LENGTH_LONG).show();
break;
case R.id.search:
String ke=key.getText().toString().trim();
Cursor cursor=dbHelper.getReadableDatabase().rawQuery("select*from dict where word like? or details like?", new String[]{"%"+ke+"%","%"+ke+"%"});
Bundle data=new Bundle();
data.putSerializable("date", converCursorToList(cursor));
Intent intent=new Intent(MainActivity.this,Result.class);
intent.putExtras(data);
startActivity(intent);
break;
default:break;
}
}
protected ArrayList<Map<String,String>> converCursorToList(Cursor cursor)
{
ArrayList<Map<String,String>> result=new ArrayList<Map<String,String>>();
while (cursor.moveToNext())
{
Map<String,String> map=new HashMap<String,String>();
map.put("word", cursor.getString(1));
map.put("detail", cursor.getString(2));
result.add(map);
}
return result;
}
private void insertData(SQLiteDatabase db,String word,String detail)
{
db.execSQL("insert into dict values(null,?,?)"+new String[]{word,detail});
}
@Override
public void onDestroy()
{
super.onDestroy();
if (dbHelper!=null)
dbHelper.close();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Result.java
package com.exampl.mysql;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Result extends Activity {
private TextView jieguo=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.line);
jieguo=(TextView)findViewById(R.id.jieguo);
Bundle bundle=getIntent().getExtras();
jieguo.setText(bundle.getString("date"));
}
}
改成這樣應該可以了
db.execSQL("insert into dict (word, detail) values(?,?)", new String[]{word,detail});
字符串這麼拼是不對的,new String[]{word,detail}是一個對象,拼出來的不是sql語句