程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 編程-安卓,創建數據庫時崩潰了

編程-安卓,創建數據庫時崩潰了

編輯:編程解疑
安卓,創建數據庫時崩潰了

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語句

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved