如何为数据库赋值

时间:2022-02-05 20:29:31

i was practising to create,store and get data's from database, i am successful in giving data's inside the coding's itself. but when i create an edit box and try to store data its not working. the following is my code can any one pls help me to solve the problem

我正在练习从数据库创建,存储和获取数据,我成功地在编码本身内部提供数据。但是当我创建一个编辑框并尝试存储数据时它不起作用。以下是我的代码可以任何一个请帮我解决问题

pls teach me from my code

请从我的代码中教我

{ 

   setContentView(R.layout.add);
    SQLiteDatabase MyDB= null;
    String MY_DATABASE_NAME = "add.db";
    String TableName = "names";
    String Data = "";           


    d1 =(EditText)findViewById(R.id.widget40);      
    d2 =(EditText)findViewById(R.id.widget42);      
    d3 =(EditText)findViewById(R.id.widget43);      
    d4 =(EditText)findViewById(R.id.widget45);
    d5 =(EditText)findViewById(R.id.widget46);


    MyDB = this.openOrCreateDatabase(MY_DATABASE_NAME, MODE_PRIVATE, null);
         MyDB.setVersion(1);
         MyDB.setLocale(Locale.getDefault());
         MyDB.setLockingEnabled(true);
         //create a database
         MyDB.execSQL("CREATE TABLE IF NOT EXISTS " 
                 + TableName
                 + "(firstname VARCHAR, middlename VARCHAR, lastname VARCHAR, dob INT(8), dor INT(8));" );
        // add data sets to the table
        MyDB.execSQL("INSERT INTO "+ TableName+ " (firstname, middlename, lastname, dob, dor)"+ " VALUES (' ', ' ' , ' ', , );");

        Cursor c = MyDB.rawQuery("SELECT * FROM " + TableName , null);

         int A = c.getColumnIndex("firstname");
         int B = c.getColumnIndex("middlename"); 
         int C = c.getColumnIndex("lastname");
         int D = c.getColumnIndex("dob");
         int E = c.getColumnIndex("dor");


      // Check if our result was valid.
       c.moveToFirst();
       if (c != null) {
        // Loop through all Results
        do {
         String q = c.getString(A);
         String w = c.getString(B);
         String e = c.getString(C);
         int r = c.getInt(D);
         int t = c.getInt(E);

         Data =Data +q+" "+w+" "+e+" "+r+" "+t+"\n";

        }while(c.moveToNext());
       }
       TextView tv = new TextView(this);
       tv.setText(Data);
       setContentView(tv);
    } 
}

i well know that it will crash. pls help me

我知道它会崩溃。请帮助我

1 个解决方案

#1


0  

Download the Zip file from this link and check there is Database creation,Edit and Delete..

从此链接下载Zip文件,检查是否有数据库创建,编辑和删除..

#1


0  

Download the Zip file from this link and check there is Database creation,Edit and Delete..

从此链接下载Zip文件,检查是否有数据库创建,编辑和删除..