我不知道错误的解决方法是什么原因我已经提到了列名,我的数据库中有一个名称列。(我只是初学者)请帮帮我。
这是我的DbH课程
package sjdb.db;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by user on 30/09/2016.
*/
public class DbH extends SQLiteOpenHelper {
private Context mycontext;
private String DB_PATH = "/data/data/sjdb.db/databases/";
private static String DB_NAME = "red.db";
private static String DB_TABLE="form";
public static String Col_1="name";
public static String Col_2="address";
public static String Col_3="number";
public SQLiteDatabase myDataBase;
/*private String DB_PATH = "/data/data/"
+ mycontext.getApplicationContext().getPackageName()
+ "/databases/";
*/
public DbH(Context context) throws IOException {
super(context,DB_NAME,null,1);
this.mycontext=context;
}
public void createdatabase() throws IOException{
boolean dbexist = checkdatabase();
if(dbexist)
{
System.out.println(" Database exists.");
}
else{
this.getReadableDatabase();
try{
copydatabase();
}
catch(IOException e){
throw new Error("Error copying database");
}
}
}
private boolean checkdatabase() {
//SQLiteDatabase checkdb = null;
boolean checkdb = false;
try{
String myPath = DB_PATH + DB_NAME;
File dbfile = new File(myPath);
checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE) != null;
checkdb = dbfile.exists();
}
catch(SQLiteException e){
System.out.println("Database doesn't exist");
}
return checkdb;
}
private void copydatabase() throws IOException {
//Open your local db as the input stream
InputStream myinput = mycontext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outfilename = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myoutput = new FileOutputStream(outfilename);
// transfer byte to inputfile to outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myinput.read(buffer))>0)
{
myoutput.write(buffer,0,length);
}
//Close the streams
myoutput.flush();
myoutput.close();
myinput.close();
}
public void opendatabase() throws SQLException
{
//Open the database
String mypath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READONLY);
}
public synchronized void close(){
if(myDataBase != null){
myDataBase.close();
}
super.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// This will return a cursor containing database records
public Cursor data(){
Cursor c;
String[] from = new String[] {Col_1, Col_2, Col_3};
c=myDataBase.query(DB_TABLE,from,null, null,null,null,null);
return c;
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
}
这是我的MyActivity类
package sjdb.db;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.IOException;
public class MyActivity extends Activity implements View.OnClickListener {
/** Called when the activity is first createdl. */
Cursor cur;
TextView tv, tv1, tv2;
DbH db;
Button next,back;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
tv=(TextView)findViewById(R.id.text);
tv1=(TextView)findViewById(R.id.text1);
tv2=(TextView)findViewById(R.id.text2);
next=(Button)findViewById(R.id.next);
back=(Button)findViewById(R.id.back);
next.setOnClickListener(this);
back.setOnClickListener(this);
try {
db=new DbH(this);
} catch (IOException e2) {
e2.printStackTrace();
}
try {
db.createdatabase();
} catch (IOException e) {
e.printStackTrace();
}
db.opendatabase();
cur=db.data();
cur.moveToFirst();
tv.setText(cur.getString(1));
tv1.setText(cur.getString(2));
tv2.setText(cur.getString(3));
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.next :
if(cur.isLast())
{
cur.moveToFirst();
tv.setText(""+cur.getString(1));
tv1.setText(""+cur.getString(2));
tv2.setText(""+cur.getString(3));
}
else
{
cur.moveToNext();
tv.setText(""+cur.getString(1));
tv1.setText(""+cur.getString(2));
tv2.setText(""+cur.getString(3));
}
break;
case R.id.back:
{
if(cur.isFirst())
{
cur.moveToLast();
tv.setText(""+cur.getString(1));
tv1.setText(""+cur.getString(2));
tv2.setText(""+cur.getString(3));
}
else {cur.moveToPrevious();
tv.setText(""+cur.getString(1));
tv1.setText(""+cur.getString(2));
tv2.setText(""+cur.getString(3));
}
break;
}
}
}
}
这是错误:
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{sjdb.db/sjdb.db.MyActivity}: android.database.sqlite.SQLiteException: no such column: name: , while compiling: SELECT name, address, number FROM form
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
at android.app.ActivityThread.access$600(ActivityThread.java:128)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: no such column: name: , while compiling: SELECT name, address, number FROM form
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1686)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1571)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1527)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1607)
at sjdb.db.DbH.data(DbH.java:122)
at sjdb.db.MyActivity.onCreate(MyActivity.java:47)
at android.app.Activity.performCreate(Activity.java:4470)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
at android.app.ActivityThread.access$600(ActivityThread.java:128)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)
I/Process: Sending signal. PID: 11719 SIG: 9
答案 0 :(得分:0)
column: name: , while compiling: SELECT name, address, number FROM form
如果没有转到TextView,请尝试使用此代码。